Convert list in Python to dictionary

Source: Internet
Author: User
Question 1: How to convert a list into a dictionary?

problem Description: For example, in Python I have a list of the following, where the odd position corresponds to the dictionary key , the even position of the correspondingvalue

Solution:

1. Using the ZIP function to implement

2. Using loops to achieve

3. Using enumerate function generation index to achieve

Question 2 How do we convert two lists into a dictionary?

problem Description: Suppose you have twolist

Solution: still a common zip function


Here we see zip that the function does a good job on pairing, and if two lists are large, you need itertools.izip to introduce them to solve the problem.

or the following direct use dict function

So what if we had three lsit? For example, we sometimes encounter such problems such as in a latitude and longitude to record a certain data, this time how to implement it?

We can see at this time that the zip function can still help us to achieve the required function successfully, first of all the latitude and longitude one by one pairing together, then the Val together, and finally use dict the function together.

Summarize

Through the above example, we know that the zip function can be used multiple times to consolidate the data, finally solve the problem, the above is the entire content of this article, I hope to learn Python can help you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.