Home  >  Article  >  Backend Development  >  How to change dict into list in python

How to change dict into list in python

藏色散人
藏色散人Original
2019-06-26 10:25:384017browse

How to change dict into list in python

How to change dict (dictionary) into list (list) in python?

Note: List cannot be converted into dictionary

①The converted list is an 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:

字典中的key转换为列表:['a','b','c']
字典中的value转换为列表:[1,2,3]

②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:

字典中的key转换为列表:['b','a','c','p','j']
字典中的value转换为列表:[2,1,3,5,4]

Note: The Python version used here is 3.x.

Related recommendations: "Python Tutorial"

The above is the detailed content of How to change dict into list in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn