简体   繁体   中英

How do i get the key at a specific index in a python dictionary?

So i need a function to delete a key at a specific index. Is there a built-in function which does that, or do i have to do it on my own?

Example:

my_dict={"bus":"2$","uber":"5$","taxi":"5$"}

var1 = my_dict.function(0)
var2 = my_dict.function(2)
var3 = my_dict.function(1)

In this case, var1 would be "bus", var2 would be "taxi" and var3 would be "uber".

You can turn all the keys into a list then index based on that.

my_dict={"bus":"2$","uber":"5$","taxi":"5$"}
var1 = list(my_dict.keys())[0]

out

bus

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