Several methods of Python dictionary traversal

Source: Internet
Author: User

(1) Traversing the key value
>>>a{'a':'1','b':'2','C':'3'}>>> forKeyinchA:Print(key+':'+A[key]) A:1B:2C:3>>> forKeyinchA.keys ():Print(key+':'+A[key]) A:1B:2C:3

On use, for keys in a and for key in A.keys (): fully equivalent.

(2) Traversal value value
 for inch a.values ():        Print (value) 123

(3) Traversing dictionary items

 >>> for  kv in   A.items ():  print   (KV)   (  " a  , "  1   "   " b  , "  2   "   " c  , "  3   ) 

(4) Traversal dictionary health value

 for inch A.items ():        Print (key+':'+value) A:1B:2C: for in A.items ():        Print (key+':'+value) A:1B:2C:3

The use of the For Key,value in A.items () is fully equivalent to the for (Key,value) in A.items ()

Several methods of Python dictionary traversal

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.