enumerate() for dictionary in python

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

    Python3:

    One solution:

    enumm = {0: 1, 1: 2, 2: 3, 4: 4, 5: 5, 6: 6, 7: 7}
    for i, k in enumerate(enumm):
        print("{}) d.key={}, d.value={}".format(i, k, enumm[k]))
    

    Output:
    0) enumm.key=0, enumm.value=1
    1) enumm.key=1, enumm.value=2
    2) enumm.key=2, enumm.value=3
    3) enumm.key=4, enumm.value=4
    4) enumm.key=5, enumm.value=5
    5) enumm.key=6, enumm.value=6
    6) enumm.key=7, enumm.value=7
    

    An another example:

    d = {1 : {'a': 1, 'b' : 2, 'c' : 3},
         2 : {'a': 10, 'b' : 20, 'c' : 30}
        }    
    for i, k in enumerate(d):
            print("{}) key={}, value={}".format(i, k, d[k])
    

    Output:    
        0) key=1, value={'a': 1, 'b': 2, 'c': 3}
        1) key=2, value={'a': 10, 'b': 20, 'c': 30}
    
    0 讨论(0)
  • 2020-12-08 06:24

    enumerate() when working on list actually gives the index and the value of the items inside the list. For example:

    l = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    for i, j in enumerate(list):
        print(i, j)
    

    gives

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

    where the first column denotes the index of the item and 2nd column denotes the items itself.

    In a dictionary

    enumm = {0: 1, 1: 2, 2: 3, 4: 4, 5: 5, 6: 6, 7: 7}
    for i, j in enumerate(enumm):
        print(i, j)
    

    it gives the output

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

    where the first column gives the index of the key:value pairs and the second column denotes the keys of the dictionary enumm.

    So if you want the first column to be the keys and second columns as values, better try out dict.iteritems()(Python 2) or dict.items() (Python 3)

    for i, j in enumm.items():
        print(i, j)
    

    output

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

    Voila

    0 讨论(0)
  • 2020-12-08 06:26

    On top of the already provided answers there is a very nice pattern in Python that allows you to enumerate both keys and values of a dictionary.

    The normal case you enumerate the keys of the dictionary:

    example_dict = {1:'a', 2:'b', 3:'c', 4:'d'}
    
    for i, k in enumerate(example_dict):
        print(i, k)
    

    Which outputs:

    0 1
    1 2
    2 3
    3 4
    

    But if you want to enumerate through both keys and values this is the way:

    for i, (k, v) in enumerate(example_dict.items()):
        print(i, k, v)
    

    Which outputs:

    0 1 a
    1 2 b
    2 3 c
    3 4 d
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-08 06:28
    1. Iterating over a Python dict means to iterate over its keys exactly the same way as with dict.keys()
    2. The order of the keys is determined by the implementation code and you cannot expect some specific order:

      Keys and values are iterated over in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and deletions. If keys, values and items views are iterated over with no intervening modifications to the dictionary, the order of items will directly correspond.

    That's why you see the indices 0 to 7 in the first column. They are produced by enumerate and are always in the correct order. Further you see the dict's keys 0 to 7 in the second column. They are not sorted.

    0 讨论(0)
提交回复
热议问题