I have a dictionary of dictionaries that looks like this:
{'info': {'status': 'OK',
'data': {'account': [{'currency': 'USD',
'name': 'United States Dollar',
'amount': '100'
'used': '20',
'total': '120'},
{'currency': 'EUR',
'name': 'Euro',
'amount': '150',
'used': '35',
'total': '185'}]}},
'USD': {'amount': 100, 'used': 20, 'total': 120},
'EUR': {'amount': 150, 'used': 35, 'total': 185},
'amount': {'USD': 100, 'EUR': 150},
'used': {'USD': 20, 'EUR': 35},
'total': {'USD': 120, 'EUR': 185}}
What I want to get from this is a currency list:
currency_list = ['USD','EUR']
and I would like to get a currency name list:
currency_list = ['United States Dollar','Euro']
How can I access the dictionaries?
Thank you
CodePudding user response:
given that this info you posted in a var dic
just do:
currency_list = [value["currency"] for value in dic["info"]["data"]["account"]]
currency_list_name = [value["name"] for value in dic["info"]["data"]["account"]]
CodePudding user response:
You can access a dictionary of dictionaries by calling the dictionary with both keys you need to use. For example currency_list[info][status] would return 'OK'. The way you have your dictionary set up is a bit confusing. I might restructure it in order to access each element in simpler fashion.
CodePudding user response:
If your dictionary is saved in a variable named yourdictionary you can use the following code.
data = yourdictionary['info'].get('data').get('account')
currency_list = [dat['currency'] for dat in data]
currency_name_list = [dat['name'] for dat in data]
Here list comprehension is used to locate all currencies and names in the dictionary.