简体   繁体   中英

Extract values from list of dictionaries

I have the following list of dictionaries from which I would like to the values of each dictionary separately.

stocks=[{'BEST':'GOOG.O','BEST':'FB.O'},
              {'REST':'SAN.MC','REST':'CDCUF.PK','REST':'EDN.BA'}]

the desired output would be:

list1=['FB.O','GOOG.O']
list2= ['SAN.MC','CDCUF.PK','EDN.BA']

I thought it was going to ok to do the following:

dict1=stocks[0]
best_sto=list(dict1.values())

dict2=stocks[1]
rest_sto_rest=list(dict2.values())

However from dict1 the output is ['FB.O'] and from dict2 returns ['EDN.BA']

The dictionaries you provide all have the same key for all values. Only one of the values will be kept, and it is the last one.

For dicts, every key must be unique. They are not multi-maps to support multiple identical keys.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM