Convert a dictionary to a list in Python

Source: Internet
Author: User
This article describes how to convert a dictionary to a list in Python. For more information, see the following article.

Note: the list cannot be converted to a dictionary.
① The converted list is unordered.

A = {'a': 1, 'B': 2, 'C': 3} # Convert the key in the dictionary to list key_value = list (. keys () print ('convert the key in the dictionary to the list: ', key_value) # convert the value in the dictionary to the list value_list = list (. values () print ('value in the dictionary is converted to the list: ', value_list)

Running result:

② The converted list is an ordered list

Import collectionsz = collections. orderedDict () z ['B'] = 2z ['A'] = 1z ['c'] = 3z ['R'] = 5z ['J'] = 4 # key in the dictionary convert to list key_value = list (z. keys () print ('convert the key in the dictionary to the list: ', key_value) # convert the value in the dictionary to the list value_list = list (z. values () print ('value in the dictionary is converted to the list: ', value_list)

Running result:

Note: The Python version 3.xis used here.

For more articles about converting a dictionary to a list in Python, refer to the PHP Chinese website!

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.