enumerate() for dictionary in python

前端 未结 11 1237
走了就别回头了
走了就别回头了 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:26

    Since you are using enumerate hence your i is actually the index of the key rather than the key itself.

    So, you are getting 3 in the first column of the row 3 4even though there is no key 3.

    enumerate iterates through a data structure(be it list or a dictionary) while also providing the current iteration number.

    Hence, the columns here are the iteration number followed by the key in dictionary enum

    Others Solutions have already shown how to iterate over key and value pair so I won't repeat the same in mine.

提交回复
热议问题