Iterative __python of dictionaries (dict) in Python

Source: Internet
Author: User
#!/usr/bin/python
#coding: utf-8

d = {' Adam ': $, ' Lisa ': ", ' Bart ':", "Paul": "The", "
the key of the iteration dict" for
x i n D.keys ():
    print x

# can also be used in this way to iterate
for x in D.iterkeys ():

The value of the dict of the print X # Iteration for
x in D.values (): C10/>print x

for X in D.itervalues ():
    print x

# Iteration key value pair for
K, v. in D.items ():
    print K, ":", v
 
  for K, V in D.iteritems ():
    print K, ":", v

' "The
above code, there are two ways to iterate between a key value or a key value, in the case of values
and Itervalues,
explain the difference between
the two ways 1. The values () method actually converts a dict into a list containing value.
2. However, the Itervalues () method does not convert, and it takes value out of the dict in turn,
so the Itervalues () method saves the memory needed to generate the list, rather than the values () method.
3. Print itervalues () finds that it returns a <dictionary-valueiterator> object, which shows that in Python the for
Loop can act as an iterative object far more than List,tuple, Str,unicode,dict and so on,
any iteration object can be used for a for loop, and internal how to iterate we don't usually care about
print d.itervalues ()
# < Dictionary-valueiterator object at 0x0137f8a0>


about iterations
If an object says that it can iterate, then we iterate over it directly with a for loop, so that the iteration is an abstract data operation,
It does not have any requirements for the data inside the iteration object.
'''
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.