Conversion of dictionaries to lists in Python, and conversion of dictionary lists in python
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.