How to convert 3 list into dictionary in python

Different ways to convert a Python List to a Dictionary

Converting a Python list to a dictionary

How to convert 3 list into dictionary in python

Photo by Corinne Kutz on Unsplash

In this article, we will discuss the different ways of converting a Python list into a dictionary.In most of the situations, one of these methods will in come handy.There is no right or wrong method here. As long as we get the expected output we can choose any solution that fits best.

Conventional for loop

First, we will look at the conventional for loop method of converting a Python list to dictionary. Although, this is not the most pythonic way, it’s indeed one of the ways to convert.

Output:{1: 'a', 'a': 2, 2: 'b', 'b': 3, 3: 'c', 'c': 4, 4: 'd'}

Using dict comprehension in the conventional method

Now, we will make it look a little pythonic by modifying the above code to a dictionary comprehension in Python.Of course we reduce the number lines of code significantly.

print({l[i]:l[i+1] for i in range(len(l)-1)})

Using zip and list slicing

This is my most favourite method of converting lists to a dictionary.We will be taking advantage of list slicing here.

Lets break down the code line by line.

Step 1: In line 2, We slice the original list l into a copy of sub-list of all elements starting from 1(index 0) with a step value of 2.This means, all the elements of even indices i.e 0,2,4,6 till the end of the list is converted to a sub-list l1.

Step 2: Similarly, all the odd indices i.e 1,3,5 etc are made into a sub-list l2

Step 3: We use zip function to pair the items in both the lists. The zip function is a zip object which contains an iterator of tuples where the first item in each iterator is paired together and then the second item is paired together and so on.

A gist of zip function in Python

For instance, we have 2 tuples t1 = (name,age) and t2 =(“John”,33) and pass them to a zip function, we get a zip object in Python 3. If we convert that to a list we get,

t1 = (name,age)
t2 =(“John”,33)
print(list(zip(t1,t2)))Output:
[('name', 'John'), ('age', 33)]

If we convert the same zip object to a dictionary we get,

t1 = (name,age)
t2 = (“John”,33)
print(dict(zip(t1,t2)))Output:
{'name': 'John', 'age': 33}

Please note that in Python 2, the zip function returns a list of tuples. This has been changed in Python 3 to return iterator of tuples to save memory.

https://docs.python.org/3.3/library/functions.html#zip

https://docs.python.org/2/library/functions.html#zip

Using zip and iter

This is almost similar to the zip function method we discussed before. However, a small change is, instead of slicing the original list we pass it to an iter function.

The iter function uses an internal count variable to track the iteration.

Once the traversal is done the count is set to 0 and cannot be reassigned.Therefore it can be used to traverse the container ONLY once.

Output:
{1: 'a', 2: 'b', 3: 'c', 4: 'd'}

How does this work?

Photo by Ben White on Unsplash

Since the iterator traverses over the items only once, the moment an element gets traversed, it gets removed. For instance, in the first iteration,the first next() returns the element 1 and then the second next() returns “a ” which gets paired by zip function into (1,a) and so on.

# This is how the above code works internally.zip(next(it),next(it))....Iteration 1: ele1 = next(it) # Returns 1
ele2 = next(it) # Returns a
zip(ele1,ele2) # Returns 1,a#Output:
1,a
Iteration 2:ele1 = next(it) # Returns 2
ele2 = next(it) # Returns b
zip(ele1,ele2) # Returns 2,b#Output:
2,b

The above process is repeated till the end of the list.

https://docs.python.org/3/library/functions.html#iter

Using fromkeys()

This method does not exactly convert a given list of values into key value pairs respectively. However, if we have to convert the values in a list to keys in a dictionary , this can be made use of. By default, all keys will have None as value if no value is specified.

Output:
{'name': None, 'address': None, 'age': None, 'sex': None}

If a value is specified in fromkeys(), then the specified value is assigned as values to all the keys.

How do I convert a list to a dictionary in Python?

Since python dictionary is unordered, the output can be in any order. To convert a list to dictionary, we can use list comprehension and make a key:value pair of consecutive elements. Finally, typecase the list to dict type.

Can you have 3 items in a dictionary Python?

You can add as many items as you like.

How do I convert a list to a dictionary key?

To convert a list to a dictionary using the same values, you can use the dict. fromkeys() method. To convert two lists into one dictionary, you can use the Python zip() function. The dictionary comprehension lets you create a new dictionary based on the values of a list.

How do you combine 3 dictionaries?

Below are the eight standard methods by which you can merge two dictionaries in python..
1) Using update() method..
2) Using merge(|) operator..
3) Using ** operator..
4) Unpacking the second dictionary..
5) Using collection.ChainMap() method..
6) Using itertools. ... .
7) Using dictionary comprehension..
8) Add values of common keys..