In this Python tutorial, we will discuss Python creates a dictionary from two lists. Here we will also cover the below examples:
- How to create a dictionary from two lists in python
- Python creates a dictionary from multiple lists
- Python generate dict from two lists
- Python created nested dictionary from two lists
- Python program to create a dictionary from two lists without losing duplicate values
Python creates a dictionary from two lists
- Here we can see how to declare a dictionary from two lists in Python.
- To do this task we will create the first list that contains the dictionary key’s and in the other list, we will contain the values element. In Python to create a list, we should use square brackets [] and put the elements in it. Each element is represented by a comma.
- There are multiple methods to perform this task
- By using dictionary comprehension method
- By using zip() and dict() method
- By using for loop and range method
Examples:
Let’s take an example and check how to create a dictionary from two lists in Python by using a dictionary comprehension method
Code:
new_lis1 = ["Python", "Java", "Ruby"]
new_lis2 = [78,92,34]
new_dict = {new_lis1[i]: new_lis2[i] for i in range(len(new_lis1))}
print ("Created dictionary:",new_dict)
In Python, the dictionary comprehension method is very easy to convert two lists to a dictionary because it saves time and it is faster than the list comprehension method.
In the above code first, we will initialize lists and put the elements in them. Now use the dictionary comprehension method to convert lists to dictionaries.
Here is the implementation of the following given code
By using zip() and dict() method
The zip() method takes multiple iterable objects as arguments such as lists and tuples and returns an iterator. In Python, the dict() method creates an empty dictionary. In this example, the dict and zip() method join together to convert lists into dictionaries.
Syntax:
dict(zip(keys,values))
Source Code:
new_ke_lis = ['b', 'z', 'a']
new_val_lis = [43, 128, 987]
new_dic_lis = dict(zip(new_ke_lis, new_val_lis))
print("Convert lists into dict",new_dic_lis)
Here is the output of the following given code
By using for loop and range method
Here we can see how to put the elements in a for loop to create a pair of values and keys. In Python the len() method returns the number of elements and the range() function is used to create a collection of numbers.
Example:
my_new_list1 = ["Japan","China","turkey"]
lis2 = [18,17,14]
new_out = {my_new_list1[m]: lis2[m] for m in range(len(my_new_list1))}
print("List converted into:",new_out)
Here is the execution of the following given code
This is how to create a dictionary from two lists in Python.
Read: Python dictionary pop
Python creates a dictionary from multiple lists
In python to convert multiple lists into a dictionary, we can easily use the zip method. The zip() method helps the user to combine three lists together and also we can use the list comprehension method to convert the lists into a dictionary
Source Code:
stu_name = ["Smith", "Gayle", "Potter", "Anderson"]
stu_id = [39,48,72.86]
stu_post = ["Developer", "gamer", "tester", "writer"]
b = [{'stu_name': stu_name, 'stu_id': stu_id, 'stu_post': stu_post} for stu_name,stu_id,stu_post in zip(stu_name,stu_id,stu_post)]
print("New dictionary:",b)
In the above code first, we will create multiple lists ‘stu_name’, ‘stu_id’, ‘stu_post’ and assign them dictionary keys and values. Now declare a variable and use the zip() and list comprehension method to convert lists into a dictionary.
Here is the Screenshot of the following given code
This is how to create a dictionary from multiple lists.
Read: Python dictionary contains
Python generate dict from two lists
- Let us see how to generate a dictionary from two lists in Python.
- By using the zip() and dict() method we will create a dictionary from two lists. In this example, we will pass two iterable items as an argument that is ‘to_lis1’ and ‘to_lis2’. Now we will convert this iterator into a dictionary key-value element by using the dict() method.
Source Code:
to_lis1 = ['Kelvin', 'Jeans']
to_lis2 = [7,8]
comb_lis = zip(to_lis1, to_lis2)
new_dict = dict(comb_lis)
print("Generate dictionary:",new_dict)
Here is the execution of the following given code
Read: Python dictionary comprehension
Python created nested dictionary from two lists
Let us see how to create a nested dictionary from two lists by using zip() and list comprehension method
Source Code:
new_lis = ["student_name", 'id', 'age']
second_lis = ['Elon', '2345', '78']
third_lis = [23, 45, 91]
new_output = [{u: {v: w}} for (u, v, w) in zip(new_lis, second_lis, third_lis)]
print("Nested dictionary:",new_output)
Here is the Output of the following given code
Read: Python dictionary find a key by value
Python program to create a dictionary from two lists without losing duplicate values
- Let us see how to create a dictionary from two lists without losing duplicate values in Python.
- In this example, I have two lists ‘my_dic_lists’ and ‘lis_val’. Now we create a default dict where the default value is a set then we have to add values in the form of a key.
Source Code:
from collections import defaultdict
my_dic_lis = ['Afghanistan', 'Europe', 'Spain', 'Paris']
lis_val = [1, 2, 2, 3]
new_dict = defaultdict (set)
for m, n in zip(my_dic_lis, lis_val) :
new_dict[ m ].add ( n )
print ("Dictionary created:",new_dict)
Here is the Screenshot of the following given code
You may also like reading the following articles.
- Python convert dictionary to list
- Python dictionary remove
- Python Absolute Value
- Python dictionary length
- Python Dictionary index
- Python dictionary of tuples
- Get all values from a dictionary Python
- Python convert dictionary to an array
- How to create an empty Python dictionary
In this Python tutorial, we have discussed Python creates a dictionary from two lists. And here we have also covered the below examples:
- Python creates a dictionary from multiple lists
- Python generate dict from two lists
- Python created nested dictionary from two lists
- Python program to create a dictionary from two lists without losing duplicate values
Entrepreneur, Founder, Author, Blogger, Trainer, and more. Check out my profile.