I have a dictionary(see below) with string as key and list as the values. How can I reference the contents of the list to get the key?
number_dict = {'digits': ['one', 'two', 'three'],
'tens': ['ten','twenty','thirty','forty'],
'hundreds':['hundred']}
so I tried
if number_dict.values()[0]=="one":
print(number_dict.key[0])
and I would have expected "digits" as an output.Instead I get:
TypeError: 'dict_values' object is not subscriptable
If it was me I would opt for a List Comprehension :
number_dict = {'digits': ['one', 'two', 'three'],
'tens': ['ten','twenty','thirty','forty'],
'hundreds':['hundred']}
[key for key, value in number_dict.items() if 'two' in value]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.