How to Create a Dictionary from one list in Python [7 Methods]

In this Python blog, I will explain how to create a dictionary from one list in Python using different methods with some illustrative examples.

As a Developer, while making the Python Project I got the requirement to create a Dictionary from one Python list. Where I have to use elements from the Python list as either keys or values in the dictionary.

Methods to 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:

  1. Dictionary Comprehension
  2. the dict.fromkeys() Method
  3. the zip() Function
  4. Using Itertools’ count()
  5. Using a Simple Loop
  6. Using tuples
  7. Using counter function from collection module

Let’s see them one by one with some demonstrative examples:

Method 1: Dictionary comprehension to creating a dictionary from a list in Python

Dictionary Comprehension is a concise way to create dictionaries using a single line of code in Python. It is similar to list comprehension but produces a Python dictionary instead of a Python list.

Context: Consider a situation where we have to create a dictionary in Python where key values are stored in the Python list.

states = ['CA', 'TX', 'NY', 'FL', 'IL']
state_index = {state: index for index, state in enumerate(states)}
print(state_index)
print(type(state_index))

Output: We aim to create a dictionary from a list in Python where each list item is a key and its position (or index) in the list is the value. The enumerate(list) iterates over the list while also keeping track of each item’s index. While the dictionary comprehension constructs a key-value pair for each state and its index.

{'CA': 0, 'TX': 1, 'NY': 2, 'FL': 3, 'IL': 4}
<class 'dict'>
how to create a dictionary from one list in Python

This way we can use dictionary comprehension to create a dictionary from one list in Python.

Method 2: Create a dictionary from a list using Python dictionary fromkeys

The dict.fromkeys() method is a built-in way to create a new Python dictionary with keys from an iterable (like a list). All the keys in the resulting dictionary have the same value, which is provided as the second argument to this Python method.

Example: Assume we want to create a dictionary that has keys from a list in Python and has all the values as ‘Not Visited’.

states = ['CA', 'TX', 'NY', 'FL', 'IL']
visited_status = dict.fromkeys(states, 'Not Visited')
print(visited_status)
print(type(visited_status))

Output: The dict.fromkeys initializes a new dictionary in Python with keys from the given list. Each key is assigned the value ‘Not Visited’.

{'CA': 'Not Visited', 'TX': 'Not Visited', 'NY': 'Not Visited', 'FL': 'Not Visited', 'IL': 'Not Visited'}
<class 'dict'>
Create a dict from a Python list

This way we can use the dict.fromkeys() methods to create a dictionary from one list in Python.

Method 3: Python create dictionary using list using the zip() function

The zip() function is a built-in Python function that pairs elements from multiple iterables. When used with the dict() constructor, it can produce a dictionary in Python. The first iterable provides the keys, and the second iterable provides the corresponding values.

Example: Using two lists in Python and combining them into a Python dictionary key and their respective values using the dict() constructor and zip() function.

abbreviations = ['CA', 'TX', 'NY', 'FL', 'IL']
full_names = ['California', 'Texas', 'New York', 'Florida', 'Illinois']
state_names = dict(zip(abbreviations, full_names))
print(state_names)
print(type(state_names))

Output: The zip(list1, list2) function pairs each list1 item with its corresponding list2 items. The dict(zip(list1, list2)) then converts these pairs into a dictionary in Python.

{'CA': 'California', 'TX': 'Texas', 'NY': 'New York', 'FL': 'Florida', 'IL': 'Illinois'}
<class 'dict'>
Python create dictionary from a list

This way we can use the zip() function to create a dictionary from one list in Python.

Method 4: Python list to dictionary using count() function.

The count() function from the itertools module produces an infinite sequence of numbers. When it is combined with the zip() function, we can associate each item from a list with a consecutive number.

Scenario: We have to assign a rank to each element in the Python list based on their index number, starting with 1 being the earliest in a Python dictionary.

from itertools import count

states = ['DE', 'PA', 'NJ', 'GA', 'CT']
state_rank = dict(zip(states, count(1)))
print(state_rank)
print(type(state_rank))

Output: The count(1) from the itertools module generates an infinite sequence of numbers starting from 1. We pair each item from the list with its rank using zip(list, count(1)). The resulting pairs are then transformed into a dictionary in Python.

{'DE': 1, 'PA': 2, 'NJ': 3, 'GA': 4, 'CT': 5}
<class 'dict'>
Python list into a dictionary

This way we can use the count() function from itertools to create a dictionary from one list in Python.

Method 5: Create a Python dictionary from list using a for loop

This method uses a straightforward for-loop to create a dictionary in Python. We iterate over the list items and their indices using enumerate(). For each iteration, the Python list item is set as a key in the Python dictionary, and its index is set as the corresponding value.

Scenario: Consider a situation where, we have to create a dictionary where the keys are indices starting from 1, and the values are the items from the Python list.

attractions = ['Golden Gate Bridge', 'The Alamo', 'Statue of Liberty', 'Disney World', 'The Bean']
state_attractions = {}
for index, attraction in enumerate(attractions):
    state_attractions[index+1] = attraction

print(state_attractions)

Output: The enumerate() is a built-in Python function that returns an iterator, which produces pairs containing the index (starting from 0 by default) and the value from the iterating list in Python.

{1: 'Golden Gate Bridge', 2: 'The Alamo', 3: 'Statue of Liberty', 4: 'Disney World', 5: 'The Bean'}
Python list into a dictionary

This way we can use a for loop in Python to create a dictionary from a list.

Method 6: Create dictionary in Python with the list using tuples

There are times when a more complex dictionary structure is desired, like having dictionaries as values. Here, we have a Python list that consists of tuples. Each tuple has two elements. Then we will use the dict constructor, the first element of each tuple is used as the outer dictionary’s key. The second element of the Python tuple is set as the value of an inner key (‘value’) inside a nested dictionary.

Example: Let’s take an example and check how to create a dictionary from one list in Python using a tuple.

Country_name = [(67, 'U.S.A'), (25, 'France'), (96, 'Germany'), (15, 'Australia'), (34, 'China')]
dict_store = dict(Country_name)
print("Created Dictionary :", dict_store)
print(type(dict_store))

Output: The dict() function is a built-in Python function that creates a dictionary from a list of tuples. Each first element of the tuple becomes a key in the dictionary, and each second element of the tuple becomes the corresponding value.

Created Dictionary : {67: 'U.S.A', 25: 'France', 96: 'Germany', 15: 'Australia', 34: 'China'}
<class 'dict'>
create Python dict from list

This way we can use a list of tuples in Python to create a dictionary.

Method 7: Create a Dictionary from one list in Python using counter()

The Counter() method from Python’s collections module, we will convert a list to a dictionary in Python 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().

from collections import Counter

bikes_in_USA = ['Harley Davidson', 'Confederate', 'Motus MSTR', 'Alta Motors Redshift']
new_dict = dict(Counter(bikes_in_USA))
print("Generated Dictionary :",new_dict )
print(type(new_dict))

Output:

Generated Dictionary : {'Harley Davidson': 1, 'Confederate': 1, 'Motus MSTR': 1, 'Alta Motors Redshift': 1}
<class 'dict'>
How to create a dictionary using list in Python

This way we can use the counter function to create a dictionary from one list in Python.

Conclusion

Understanding how to create a dictionary from one list in Python using seven different methods like dictionary comprehension, dict.fromkeys() method, zip() function, count() function, a for loop, list of tuples, and counter() function can provide many solutions to a problem. I have explained each method with the help of some examples.

Also, take a look at some more Python tutorials.