Dictionary traversal in Python

Source: Internet
Author: User
This article mainly introduces the dictionary traversal memo in Python. This article lists a variety of dictionary traversal methods, which are suitable for beginners to view and briefly explains the differences between interitems and iterms, for more information, see how to traverse the dictionary in python. it is only for my reference as a beginner.

The code is as follows:


#! /Usr/bin/env python
# Coding = UTF-8
DemoDict = {'1': 'chromi', '2': 'Android '}

For key in demoDict. keys ():
Print key

For value in demoDict. values ():
Print value

For key in demoDict:
Print key, demoDict [key]


For key, value in demoDict. items ():
Print key, value

For key in demoDict. iterkeys ():
Print key

For value in demoDict. itervalues ():
Print value

For key, value in demoDict. iteritems ():
Print key, value

Print 'dict. keys () = ', demoDict. keys (),'; dict. iterkeys () = ', demoDict. iterkeys ()

Interitems and iterms

Reference http://stackoverflow.com/questions/10458437/python-what-is-the-difference-between-dict-items-and-dict-iteritems

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.