简体   繁体   中英

how to get dictionary key inside index python

i have a data that has format like this

data_lists = [{'x': '1', 'y': '2', 'z': 3}]

how if I want to get the 'x' or 'y'?

I just trying data_lists[{'x'}] , but got error ('list indices must be integers or slices, not set',)

also with data_lists[{[0]}] , the error say ("unhashable type: 'list'",)

thank you for helping me

data_lists = [{'x': '1', 'y': '2', 'z': 3}]
#first, iterate over the list items, which in this case there is only one
for i in data_lists:
    #then, select thee key you want
    print(i['y'])

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