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.