Home > Article > Backend Development > Convert List in python into dictionary
Question 1: How to convert a list into a dictionary?
Problem description: For example, in python, I have the following list, in which the odd positions correspond to the key
of the dictionary, and the even positions correspond to value
Solution:
1. Use zip function to implement
2. Use loops to achieve
3. Use enumerate
Function generation index
to achieve
Question 2 How do we convert two lists into A dictionary?
Problem description: Suppose you have two list
Solution: is still a common zip
function
Here we see that the zip
function is indeed The pairing has a very good effect. If both lists are large, you need to introduce itertools.izip
to solve the problem
Or use the dict
function directly below
So what if we have three lsit? For example, we sometimes encounter such problems, such as recording certain data under a longitude and latitude. How to achieve this?
We can see that at this time the zip
function can still help us successfully implement the required functions. First, integrate the longitude and latitude one by one. , then connect the vals together, and finally use the dict
function to put them together.
Summary
#Through the above example, we know that we can integrate the data through multiple calls of the zip function and finally solve the problem , the above is the entire content of this article, I hope it will be helpful to everyone learning python.
For more articles related to converting List in python into dictionary, please pay attention to the PHP Chinese website!