In this Python tutorial, we will learn how to create a Python dictionary from one list. To understand various approaches, we’ll use some built-in functions to create a Dictionary from one Python list.
As a Developer, while making the Python Project I got the requirement to create a Dictionary from one Python list.
Here we will see:
- How to create a dictionary from one list in Python using dictionary comprehension
- Create a Dictionary from one list in Python using zip()
- How to create a dictionary from one list in Python using dict.fromkeys()
- Create a Dictionary from one list in Python using an enumerator()
- How to create a dictionary from one list in Python using a tuple
- Create a Dictionary from one list in Python using a counter()
Create a Dictionary from one list in Python
In Python, there are numerous methods for creating a Dictionary. We’ll go through how to create a Dictionary from one list in Python specifically utilizing the dictionary comprehension method, and zip method.
How to create a dictionary from one list in Python using dictionary comprehension
- In this section, we will discuss how to create a dictionary from one list in Python using dictionary comprehension.
- With this technique, we iterate through the provided Python list and create the corresponding key: value inside the curly brackets. Every list element turns into a dictionary key and based on the dictionary comprehension, we may generate values for each key.
- The user can utilize dictionary comprehensions in Python just like list comprehensions. By using the simple expression of the built-in technique, the user can generate the dictionary.
Syntax:
Let’s have a look at the Syntax and understand the working of dictionary comprehension
{key: 'value' (value for (key, value) in iterable form)
Example:
Let’s take an example and check How to create a dictionary from one list in Python using dictionary comprehension.
Source Code:
#Creating a list
Country_name = ['U.S.A', 'Australia', 'Germany', 'China','Argentina']
print("Input Python list: ", Country_name)
#Dictionary Comprehension
new_dict = {key: "47" for key in Country_name}
# Display the Content
print("Generated Python dictionary: ", new_dict)
In the following given code first, we created a list named ‘Country_name’ and then we used the dictionary comprehension method.
Here is the implementation of the following given code
Read: Python Dictionary Methods
Create a Dictionary from one list in Python using zip()
- Now let us see how to Create a Dictionary from one list in Python using zip().
- The two iterators are combined using the zip() function. We must first create an iterator, initialize it to any variable, and then typecast it to the dict() method.
- In this example first, we will declare an iterator by using the iter() method which will iterate the values in a list.
- After that, we’ll utilize the Python zip() function to create the corresponding key: value by actually zipping two objects together.
Syntax:
Here is the Syntax of the zip() method in Python
zip(*iterables)
Note: Zip can accept a variety of iterables as parameters, denoted by the * and the iterable can be a list.
Example:
Here we will take an example and check how to Create a Dictionary from one list in Python using zip().
Source Code:
# Creating a list
States_in_USA = ['Arkansas',71601, 'California',90001, 'Colorado', 80001]
# Display the list
print("Input list :",States_in_USA)
# Decalring an iterator for list 'States_in_USA'
iterable = iter(States_in_USA)
# Using the Python zip() function to create a dictionary from input list
new_output = dict(zip(iterable, iterable))
# Display the result
print("Created dictionary :", new_output)
In the above code first, we created an input list and then used the iter() method to iterate the values in the list. After that we used the zip() method to combine the iterators and convert them into the dictionary by using the dict() method.
Here is the Screenshot of the following given code.
Read: Python Dictionary of sets
How to create a dictionary from one list in Python using dict.fromkeys()
- Here we will discuss How to create a dictionary from one list in Python using dict.fromkeys().
- The dict.fromkeys() method builds a new dictionary using the iterable (string, list, set, or tuple) that is provided as keys and the value that is specified.
- It will accept a sequence (list) as input, along with a value that will enable us to map the sequence with the value. If no value is assigned to it, it will return a dictionary with the value “None.”
Syntax:
Let’s have a look at the Syntax and understand the working of dict.fromkeys() method.
dictionary.fromkeys(sequence, value)
- It consists of a few parameters
- sequence: a sequence or iterable, the elements of which would serve as the new dictionary’s keys.
- value: It is an optional parameter and by default, it takes none value.
Example:
Let’s take an example and check How to create a dictionary from one list in Python using dict.fromkeys().
Source Code:
# Create a list as a state in U.S.A
State_name = ["Alabama", "California", "Florida", "Hawaii"]
# Declare value
Country_name = "U.S.A"
# By using dict.fromkeys() method
new_result = dict.fromkeys(State_name, Country_name)
# Display the dictionary
print("Created a Dictionary :",new_result)
In this example, we used the concept of dict.fromkeys() method from converting the input list into a dictionary.
You can refer to the below Screenshot.
Read: Python dictionary multiple values
Create a Dictionary from one list in Python using an enumerator()
- In this section, we will discuss how to Create a Dictionary from one list in Python using an enumerator().
- Enumerate() is a built-in Python function that is accessed through the Python library. It accepts a collection or tuples as input and returns it as an enumerate object.
- This enumerate() function may be used instantly in loops to transform our list into a dictionary using the dict method.
Syntax:
Here is the Syntax of enumerate() method in Python
enumerate(iterable, start=0)
- It consists of a few parameters
- iterable: an object that can be looped.
- start: By default, it takes a 0 value For the first item in the loop, the count will begin with the value specified in the startIndex, and it will increase for each following item until it reaches the end of the loop.
Example:
Here we will take an example and check how to Create a Dictionary from one list in Python using an enumerator().
Source Code:
# Create a list
Country_name = ['U.S.A', 'Australia', 'Germany', 'China','Argentina']
# Enumerate method
new_val = enumerate(Country_name, 3)
# Dict method
new_result = dict(new_val )
# Display the Content
print(new_result )
In the above code first, we created the input list named and After generating an enumerate object, we must transform it into a dictionary in order to convert a list to a dictionary. We may transform our enumerate object into a dictionary by utilizing typecasting function.
Here is the implementation of the following given code.
Read: Python dictionary of lists
How to create a dictionary from one list in Python using a tuple
- Now let’s see How to create a dictionary from one list in Python using a tuple.
- Here, we will first generate a list of tuples, where each tuple has (key, value) elements, and then transform that list of tuples into one dictionary by using the dict function.
Example:
Let’s take an example and check how to create a dictionary from one list in Python using a tuple.
Source Code:
# Declare a list of tuple
Country_name = [(67, 'U.S.A'), (25, 'France'), (96, 'Germany'),
(15, 'Australia'), (34, 'China')]
dict_store = {}
# By using type casting method
dict_store = dict(Country_name)
# Display the Content
print("Created Dictionary :", dict_store)
In the above code first, we declared a list of tuples and then used the dict() method to convert them into a dictionary. After executing the code output displays the dictionary.
You can refer to the below Screenshot
Read: Get First Key in dictionary Python
Create a Dictionary from one list in Python using a counter()
- In this section, we will discuss how to Create a Dictionary from one list in Python using a counter().
- Using the Counter() method from Python’s collections module, we will convert a list to a dictionary using this method.
- The counter is an unordered collection in which the elements are stored as Dict keys and the count as a Dict value. The count of counter items can be an integer that is positive, zero, or negative.
Example:
Here we will take an example and check to Create a Dictionary from one list in Python using a counter().
Source Code:
from collections import Counter
# Creation of input list
bikes_in_USA = ['Harley Davidson', 'Confederate', 'Motus MSTR', 'Alta Motors Redshift']
new_result = Counter(bikes_in_USA)
print(new_result)
# Convert list into dictionary by using dict() method
new_dict = dict(new_result)
# Display the Content
print("Generated Dictionary :",new_dict )
Here is the Screenshot of the following given code
Also, take a look at some more Python tutorials.
- How to remove key from dictionary in Python
- How to create a dictionary from two lists in python
- How to Check if a Key Exists in a Python Dictionary
- Python get all files in directory + various examples
In the article, we have understood how to create a dictionary from one list in Python. Also we have covered the following given topics.
- How to create a dictionary from one list in Python using dictionary comprehension
- Create a Dictionary from one list in Python using zip()
- How to create a dictionary from one list in Python using dict.fromkeys()
- Create a Dictionary from one list in Python using an enumerator()
- How to create a dictionary from one list in Python using a tuple
- Create a Dictionary from one list in Python using a counter()
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.