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

后端 未结 10 598
天命终不由人
天命终不由人 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: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')
    

提交回复
热议问题