1

I'm trying to iterate over a list of dicts and get only the symbol and company name out of it maybe store in another dict, I tried parsing the json file directly and failed. I tried searching all the places I could think of and I'm stumped. Any help is greatly appreciated.

data = [{'symbol': 'ZYME', 'exchange': 'NYS', 'name': 'Zymeworks Inc.', 'date': '2019-11-06', 'type': 'cs', 'iexId': 'IEX_5253315838322D52', 'region': 'US', 'currency': 'USD', 'isEnabled': True}, {'symbol': 'ZYNE', 'exchange': 'NAS', 'name': 'Zynerba Pharmaceuticals Inc', 'date': '2019-11-06', 'type': 'cs', 'iexId': 'IEX_4E3154424A382D52', 'region': 'US', 'currency': 'USD', 'isEnabled': True}, {'symbol': 'ZYXI', 'exchange': 'NAS', 'name': 'Zynex Inc.', 'date': '2019-11-06', 'type': 'cs', 'iexId': 'IEX_4E464C4C4A462D52', 'region': 'US', 'currency': 'USD', 'isEnabled': True}]

spam = {}
def loadWords():
        for e in data:
            for k, v in e.items():
                if k == 'symbol' or k == 'name':
                    if v not in spam:
                        spam.update({k:v})
                        continue
        return spam
1

I think using a dictionary comprehension is probably easiest. I think this is the sort of thing you are trying to get?

data = [{'symbol': 'ZYME', 'exchange': 'NYS', 'name': 'Zymeworks Inc.', 'date': '2019-11-06', 'type': 'cs', 'iexId': 'IEX_5253315838322D52', 'region': 'US', 'currency': 'USD', 'isEnabled': True}, {'symbol': 'ZYNE', 'exchange': 'NAS', 'name': 'Zynerba Pharmaceuticals Inc', 'date': '2019-11-06', 'type': 'cs', 'iexId': 'IEX_4E3154424A382D52', 'region': 'US', 'currency': 'USD', 'isEnabled': True}, {'symbol': 'ZYXI', 'exchange': 'NAS', 'name': 'Zynex Inc.', 'date': '2019-11-06', 'type': 'cs', 'iexId': 'IEX_4E464C4C4A462D52', 'region': 'US', 'currency': 'USD', 'isEnabled': True}]
stocklist = []
keys = ['symbol', 'name']
for d in data:
    stock = {key: val for key, val in d.items() if key in keys}
    stocklist.append(stock)
print(stocklist)

and that will give you a list of dictionaries with only those 2 keys:

[{'symbol': 'ZYME', 'name': 'Zymeworks Inc.'}, {'symbol': 'ZYNE', 'name': 'Zynerba Pharmaceuticals Inc'}, {'symbol': 'ZYXI', 'name': 'Zynex Inc.'}]

If this is for pset8, I don't see why you need to do this, as the helpers.py lookup function parses the API results for you and returns just the values you need.

1
  • Thank you so very much @curiouskiwi this is exactly what I was looking for. You're right the helper would parse the result when we look up prices. What I'm trying to do is create a table with all the symbols and names so I can join this with the transaction table which I thought would be a better design. I don't have to repeat the full name of the company in each row when a user buys/sells, recording the ticker should be enough. Now, I could've added another query in the buy page to insert the name on the table, but, I wanted to learn a bit of json parsing. You've been a great help! – rajacs50 Nov 8 '19 at 4:23

You must log in to answer this question.

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