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!