Home > Backend Development > Python Tutorial > Convert dictionary to list in Python

Convert dictionary to list in Python

高洛峰
Release: 2017-02-24 16:15:23
Original
2298 people have browsed it

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

Running result:

Convert dictionary to list in Python

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

Convert dictionary to list in Python

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!

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