How to change dict into list in python

藏色散人
Release: 2019-08-01 15:39:19
Original
3916 people have browsed it

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)
Copy after login

Running result:

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

②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)
Copy after login

Running results:

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!