enumerate() for dictionary in python

前端 未结 11 1192
走了就别回头了
走了就别回头了 2020-12-08 05:55

I know we use enumerate for iterating a list but I tried it in a dictionary and it didn\'t give an error.

CODE:

enumm = {0: 1, 1: 2, 2:          


        
11条回答
  •  [愿得一人]
    2020-12-08 06:28

    The first column of output is the index of each item in enumm and the second one is its keys. If you want to iterate your dictionary then use .items():

    for k, v in enumm.items():
        print(k, v)
    

    And the output should look like:

    0 1
    1 2
    2 3
    4 4 
    5 5
    6 6
    7 7
    

提交回复
热议问题