Home  >  Article  >  Backend Development  >  Python how to split dictionary keys and values ​​into separate lists? (code example)

Python how to split dictionary keys and values ​​into separate lists? (code example)

青灯夜游
青灯夜游Original
2019-03-02 14:25:5724174browse

How to split a given dictionary into a list of keys and values ​​in Python? The following article will introduce you to several implementation methods. I hope it will be helpful to you. [Video tutorial recommendation: Python tutorial]

Python how to split dictionary keys and values ​​into separate lists? (code example)

Method 1: Use built-in functions: keys() and values()

keys() function: can return all the keys in a dictionary in the form of a list.

values() function: can return all the values ​​in a dictionary in the form of a list.

The following is a code example to see how to use the keys() and values() functions to split the keys and values ​​of the dictionary.

#初始化字典
ini_dict = {'a' : 'akshat', 'b' : 'bhuvan', 'c': 'chandan'}
  
# 输出ini_dict字典
print("ini_dict=", str(ini_dict))
  
# 将字典拆分为键和值的列表
keys = ini_dict.keys()
values = ini_dict.values()
  
# 分别输出键和值的列表
print ("keys : ", str(keys))
print ("values : ", str(values))

Output:

Python how to split dictionary keys and values ​​into separate lists? (code example)

##Method 2: Use zip() function

zip() function is used to take an iterable object as a parameter, pack the corresponding elements in the object into tuples, and then return a list composed of these tuples.

If the number of elements in each iterator is inconsistent, the length of the returned list is the same as the shortest object. Using the * operator, the tuple can be decompressed into a list.

The following is a code example to see how to use the zip() function to split the keys and values ​​of a dictionary.

#初始化字典
ini_dict = {'student_id' : '01', 'name' : 'May', 'age': '22'}
# 输出ini_dict字典
print("ini_dict=", str(ini_dict))

# 将字典拆分为键和值的列表
keys, values = zip(*ini_dict.items())

# 分别输出键和值的列表
print ("keys : ", str(keys))
print ("values : ", str(values))

Output:


Python how to split dictionary keys and values ​​into separate lists? (code example)

Note: In order to reduce memory in Python 3.x, zip() returns Is an object; if you need to display a list, you need to manually use list() conversion.

Method 3: Use the items() function

items() function will return a traversable (key, value) tuple in a list array.

The following is a code example to see how to use the items() function to split the keys and values ​​of the dictionary.

#初始化字典
ini_dict = {'student_id' : '05', 'name' : '欧阳克', 'age': '22'}

# 输出ini_dict字典
print("ini_dict=", str(ini_dict))

# 将字典拆分为键和值的列表
keys = []
values = []
items = ini_dict.items()
for item in items:
    keys.append(item[0]), values.append(item[1])

# 分别输出键和值的列表
print ("keys : ", str(keys))
print ("values : ", str(values))

Output:

Python how to split dictionary keys and values ​​into separate lists? (code example)

The above is the entire content of this article, I hope it will be helpful to everyone's learning. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of Python how to split dictionary keys and values ​​into separate lists? (code example). 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