Convert a dictionary to a list in Python

Source: Internet
Author: User
This article mainly introduces the method of converting a dictionary into a list in Python, and the friends you need can refer to the following

Description: The list cannot be converted to a dictionary
① converted list to unordered list

A = {' A ': 1, ' B ': 2, ' C ': 3} #字典中的key转换为列表key_value = List (A.keys ()) print (' key in dictionary is converted to list: ', Key_value) #字典中的value转换为列表value _list = List (A.values ()) print (' value in dictionary ' is converted to list: ', value_list)

Operation Result:

② the converted list to an ordered list

Import Collectionsz = collections. Ordereddict () z[' B '] = 2z[' a '] = 1z[' c '] = 3z[' r '] = 5z[' j '] = 4# Dictionary of key conversion to list Key_value = Lists (Z.keys ()) print (' key in dictionary converted to list: ', key_value) #字典中的value转换为列表value_list = List (Z.values ()) print (' value in dictionary is converted to list: ', value_list)

Operation Result:

Note: The python version used here is 3.x.

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.