0

In index, how do I create a new dict which takes all the non-zero share entries to display. For example I can use stocks = db.execute and use SUM(shares) to get the total shares for each stock. But then I want to create a new dict which only has those corresponding entries and not all the ones in the table. Do I say new_stocks = {}{} or and the populate it new_stocks[row1]["shares"] = ...

thanks for any assistance.

0

Another way to accomplish this would be to select only the rows you want in the sql. You can use a HAVING clause to filter aggregate results. Something like:

SELECT SUM(shares) as hash ,symbol
from transactions
group by symbol
HAVING hash > 0

[Note: the as hash renames SUM(shares) to hash, otherwise the key in the return dictionary will be "SUM(shares)"]

Here's the sqlite doc.

new_stocks = {}{} is not valid syntax.

If you want it to be a dict, new_stocks = {} will initialize it. Then you could iterate through stocks and update it with any stock where "SUM(shares)" is not 0.

If you want it to be a list, new_stocks = [] will initialize it. Then you could iterate through stocks and append any stock where "SUM(shares)" is not 0.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .