What is the difference between dict.items() and dict.iteritems() in Python2?

后端 未结 10 551
天命终不由人
天命终不由人 2020-11-22 08:10

Are there any applicable differences between dict.items() and dict.iteritems()?

From the Python docs:

dict.items(): Return a

相关标签:
10条回答
  • 2020-11-22 08:11

    It's part of an evolution.

    Originally, Python items() built a real list of tuples and returned that. That could potentially take a lot of extra memory.

    Then, generators were introduced to the language in general, and that method was reimplemented as an iterator-generator method named iteritems(). The original remains for backwards compatibility.

    One of Python 3’s changes is that items() now return iterators, and a list is never fully built. The iteritems() method is also gone, since items() in Python 3 works like viewitems() in Python 2.7.

    0 讨论(0)
  • 2020-11-22 08:19

    dict.items() return list of tuples, and dict.iteritems() return iterator object of tuple in dictionary as (key,value). The tuples are the same, but container is different.

    dict.items() basically copies all dictionary into list. Try using following code to compare the execution times of the dict.items() and dict.iteritems(). You will see the difference.

    import timeit
    
    d = {i:i*2 for i in xrange(10000000)}  
    start = timeit.default_timer() #more memory intensive
    for key,value in d.items():
        tmp = key + value #do something like print
    t1 = timeit.default_timer() - start
    
    start = timeit.default_timer()
    for key,value in d.iteritems(): #less memory intensive
        tmp = key + value
    t2 = timeit.default_timer() - start
    

    Output in my machine:

    Time with d.items(): 9.04773592949
    Time with d.iteritems(): 2.17707300186
    

    This clearly shows that dictionary.iteritems() is much more efficient.

    0 讨论(0)
  • 2020-11-22 08:20

    dict.iteritems is gone in Python3.x So use iter(dict.items()) to get the same output and memory alocation

    0 讨论(0)
  • You asked: 'Are there any applicable differences between dict.items() and dict.iteritems()'

    This may help (for Python 2.x):

    >>> d={1:'one',2:'two',3:'three'}
    >>> type(d.items())
    <type 'list'>
    >>> type(d.iteritems())
    <type 'dictionary-itemiterator'>
    

    You can see that d.items() returns a list of tuples of the key, value pairs and d.iteritems() returns a dictionary-itemiterator.

    As a list, d.items() is slice-able:

    >>> l1=d.items()[0]
    >>> l1
    (1, 'one')   # an unordered value!
    

    But would not have an __iter__ method:

    >>> next(d.items())
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: list object is not an iterator
    

    As an iterator, d.iteritems() is not slice-able:

    >>> i1=d.iteritems()[0]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'dictionary-itemiterator' object is not subscriptable
    

    But does have __iter__:

    >>> next(d.iteritems())
    (1, 'one')               # an unordered value!
    

    So the items themselves are same -- the container delivering the items are different. One is a list, the other an iterator (depending on the Python version...)

    So the applicable differences between dict.items() and dict.iteritems() are the same as the applicable differences between a list and an iterator.

    0 讨论(0)
  • 2020-11-22 08:21

    If you have

    dict = {key1:value1, key2:value2, key3:value3,...}

    In Python 2, dict.items() copies each tuples and returns the list of tuples in dictionary i.e. [(key1,value1), (key2,value2), ...]. Implications are that the whole dictionary is copied to new list containing tuples

    dict = {i: i * 2 for i in xrange(10000000)}  
    # Slow and memory hungry.
    for key, value in dict.items():
        print(key,":",value)
    

    dict.iteritems() returns the dictionary item iterator. The value of the item returned is also the same i.e. (key1,value1), (key2,value2), ..., but this is not a list. This is only dictionary item iterator object. That means less memory usage (50% less).

    • Lists as mutable snapshots: d.items() -> list(d.items())
    • Iterator objects: d.iteritems() -> iter(d.items())

    The tuples are the same. You compared tuples in each so you get same.

    dict = {i: i * 2 for i in xrange(10000000)}  
    # More memory efficient.
    for key, value in dict.iteritems():
        print(key,":",value)
    

    In Python 3, dict.items() returns iterator object. dict.iteritems() is removed so there is no more issue.

    0 讨论(0)
  • 2020-11-22 08:26

    dict.iteritems(): gives you an iterator. You may use the iterator in other patterns outside of the loop.

    student = {"name": "Daniel", "student_id": 2222}
    
    for key,value in student.items():
        print(key,value)
    
    ('student_id', 2222)
    ('name', 'Daniel')
    
    for key,value in student.iteritems():
        print(key,value)
    
    ('student_id', 2222)
    ('name', 'Daniel')
    
    studentIterator = student.iteritems()
    
    print(studentIterator.next())
    ('student_id', 2222)
    
    print(studentIterator.next())
    ('name', 'Daniel')
    
    0 讨论(0)
提交回复
热议问题