This article mainly introduces the method of converting a dictionary into a list in Python. Friends who need it can refer to the following
Note: Lists cannot be converted into dictionaries
①The converted list is unordered List
a = {'a' : 1, 'b': 2, 'c' : 3} #字典中的key转换为列表 key_value = list(a.keys()) print('字典中的key转换为列表:', key_value) #字典中的value转换为列表 value_list = list(a.values()) print('字典中的value转换为列表:', value_list)
Running result:
②The converted list is an ordered list
import collections z = collections.OrderedDict() z['b'] = 2 z['a'] = 1 z['c'] = 3 z['r'] = 5 z['j'] = 4 #字典中的key转换为列表 key_value = list(z.keys()) print('字典中的key转换为列表:', key_value) #字典中的value转换为列表 value_list = list(z.values()) print('字典中的value转换为列表:', value_list)
Running results:
Note: The Python version used here is 3.x.
For more articles related to converting a dictionary into a list in Python, please pay attention to the PHP Chinese website!