You can convert a Python list to a dictionary using a dictionary comprehension, dict.fromkeys(), or the zip() method. All three methods create a new dictionary. They do not modify the existing list.
Python lists and dictionaries are two structures used to store data. But what if you want to convert a list into a dictionary? You may want to do this if you want to assign a unique label to each value you have stored. This is only possible in a dictionary.
There are a few techniques and built-in functions you can use in Python to convert a list to a dictionary. This programming tutorial will discuss, with reference to examples, how to convert a list into a dictionary in Python.
Python Lists and Dictionaries
Lists and dictionaries are examples of Python collections. They allow you to store multiple similar values together in one data structure.
The list data structure allows you to store data in a specific order. Here is an example of a Python list:
takeout_prices = [2.50, 2.75, 4.50, 5.00]
Our list called takeout_prices contains four values.
Dictionaries, on the other hand, are unordered, and cannot store multiple duplicate values.
Here is an example of a Python dictionary:
takeout_prices = { 'single_chips': 2.50, 'large_chips': 2.75, 'single_fish': 4.50, 'large_fish': 5.00 }
Dictionaries are useful if you want to associate labels with each value. Lists are better if you are storing similar data, such as a list of prices or names, where you cannot label each value.
Python Convert List to Dictionary
You can convert a Python list to a dictionary using the dict.fromkeys() method, a dictionary comprehension, or the zip() method. The zip() method is useful if you want to merge two lists into a dictionary.
Let’s quickly summarize these methods:
- dict.fromkeys(): Used to create a dictionary from a list. You can optionally set a default value for all keys. You cannot set individual values for each item in the dictionary.
- Dictionary comprehension: Creates a new dictionary from an existing list. You can specify different values for each key in the dictionary. Dictionary comprehensions are similar to Python list comprehensions in structure.
- zip(): Converts two or more lists into a list of tuples. You can use the dict() method to convert the list of tuples into a dictionary.
We’re going to use each of the three methods above to demonstrate how we can convert a list into a dictionary.
Python Convert List to Dictionary: dict.fromkeys()
Say that we have a list of fruits that we want to turn into a dictionary. The value assigned to each fruit should be In stock:
fruits = ["Apple", "Pear", "Peach", "Banana"]
We could create this dictionary using the dict.fromkeys() method. This method accepts a list of keys that you want to turn into a dictionary. Optionally, you can specify a value you want to assign to every key.
You can only specify one value to be assigned to every key. You cannot say that one key should have one value and another key should have a different value.
Let’s turn our list of fruits into a dictionary:
fruits = ["Apple", "Pear", "Peach", "Banana"] fruit_dictionary = dict.fromkeys(fruits, "In stock") print(fruit_dictionary)
Our code returns the objects from this list in a dictionary object:
{'Apple': 'In stock', 'Pear': 'In stock', 'Peach': 'In stock', 'Banana': 'In stock'}
We first declare a Python variable called fruits which stores the names of the keys we want to use in our dictionary.
We use dict.fromkeys() to create a dictionary that uses the key names we stored in the fruits array. This method assigns the value of each key to be In stock. Finally, we print the new dictionary to the console.
If we did not specify the value In stock in our code, the default value for the keys in our dictionary would be None.
Convert List to Dictionary Python: Dictionary Comprehension
We can also use a technique called dictionary comprehension to convert a Python list to a dictionary using the same values.
A dictionary comprehension is similar to a list comprehension in that both methods create a new value of their respective data types. Dictionary comprehensions use pointed brackets ({}) whereas list comprehensions use square brackets ([]).
Let’s convert our list of fruits to a dictionary using dictionary comprehension:
fruits = ["Apple", "Pear", "Peach", "Banana"] fruit_dictionary = { fruit : "In stock" for fruit in fruits } print(fruit_dictionary)
Our code returns:
{'Apple': 'In stock', 'Pear': 'In stock', 'Peach': 'In stock', 'Banana': 'In stock'}
First, we declared a list called fruits which stored the names we wanted to move into a dictionary.
Then, we used dictionary comprehension to run through each item in the fruits list. We add an item to our new dictionary for each fruit in our list. The value we assigned to each fruit was In stock. Finally, we printed out our new dictionary to the console.
Python Convert List to Dictionary: zip()
In our last example, we have converted a single list into a dictionary and assigned a default value for each item in the dictionary. However, it is possible to convert two lists into a dictionary.
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot
To do so, we can use the Python zip() function. This function lets us merge two lists together. We can use one list as the keys for the dictionary and the other as the values.
Suppose we have two lists: one containing a list of fruits, and the other containing the price for an individual piece of fruit. We want to create a single dictionary that stores the name of a fruit, alongside its price. We can use the following code to accomplish this task:
fruits = ["Apple", "Pear", "Peach", "Banana"] prices = [0.35, 0.40, 0.40, 0.28] fruit_dictionary = dict(zip(fruits, prices)) print(fruit_dictionary)
Our code returns:
{'Apple': 0.35, 'Pear': 0.4, 'Peach': 0.4, 'Banana': 0.28}
First, we have specified two lists: a list of fruits, and a list of prices. Then, we have used the zip() function to merge our two lists together. The zip() function returns a list of merged tuples. Because we want a dictionary, we have used dict() to convert our tuples into a dictionary.
Next, we printed the contents of our new dictionary to the console.
Conclusion
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.
In this tutorial, we used examples to demonstrate how to use three approaches to convert a list to a dictionary in Python. Now you’re ready to start converting Python lists to dictionaries like a professional programmer!
Are you interested in learning more about Python? Read our How to Learn Python guide. This guide contains a list of online courses, books, and learning resources you can use to build your knowledge of coding in Python.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.