简体   繁体   中英

How to access the key from the tuple in python dictionary?

I have dictionary like this;

  csvDict[key] = {'delivery': delivery, 'tanksystemid': tanksystemid}

Im trying to do a condition check like;

tanksystemid=100

for dic in csvDict.values():
                if tanksystemid == dic['tanksystemid']:
                    key of csvDict?

How can i get the key of the csvDict?

You need to just use csvDict.items() instead of csvDict.values() . Like (python3) :

tanksystemid=100
for key, dic in csvDict.items():
                if tanksystemid == dic['tanksystemid']:
                    # key of csvDict?
                    print(f'key: {key}')

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