Convert list to dictionary Python


As a Python programmer, you'll often come across the need to convert a list to a dictionary.

This is a common task that can be done in multiple ways using built-in functions, loops, comprehensions, and more.

In this article, we'll explore various methods to convert a list to a dictionary in Python, and provide you with examples and use cases for each.

    Table of Contents

  1. Using for loop
  2. Using zip() function
  3. Using dictionary comprehension
  4. Using enumerate() function
  5. Using fromkeys() method
  6. Frequency Asked Questions
  7. Conclusion

Here are 5 different methods to convert a list to a dictionary in Python.

1. Using a for loop

One of the simplest methods to convert a list to a dictionary in Python is by using a for loop.

Using loop you can iterate over the list and add each item to a dictionary with a unique key. The key can be the index of the item in the list or any other unique value.

Let's see how this works.

Example
my_list = ["apple", "banana", "cherry"]
my_dict = {}

# using for loop to create a dictionary
for i in range(len(my_list)):
    my_dict[i] = my_list[i]

print(my_dict)
{0: 'apple', 1: 'banana', 2: 'cherry'}

In above example, we created an empty dictionary and used a for loop to iterate over the list. We used the index of the item in the list as the key and the item itself as the value.


2. Using zip() function

Another method to convert a list to a dictionary in Python is by using the zip() function.

The property of zip() function is that it creates an iterator that will aggregate elements from two or more iterables.

So we can combine list of keys and list of values into a single list of tuples using zip() function and then convert it into a dictionary using dict() function.

Example
keys = ["name", "age", "city"]
values = ["John", 30, "New York"]

# using zip() to convert lists to dictionary
my_dict = dict(zip(keys, values))
print(my_dict)
{'name': 'John', 'age': 30, 'city': 'New York'}

3. Using dictionary comprehension

Dictionary comprehension is a short and powerful syntax to create a dictionary in Python.

Using this method, we can create a dictionary from a list of keys and a list of values in a single line of code.

Here is an example for this:

Example
my_list = ["apple", "banana", "cherry"]

# using dictionary comprehension to create a dictionary
my_dict = {i: my_list[i] for i in range(len(my_list))}
print(my_dict)
{0: 'apple', 1: 'banana', 2: 'cherry'}

4. Using enumerate() function

Another method to convert a list to a dictionary in Python is by using the enumerate() function.

It returns an enumerate object which contains the index and value of all the items in the list as a tuple.

You can pass the enumerate object directly to the dict() function to create a dictionary.

Example
my_list = ["apple", "banana", "cherry"]

# using enumerate() to create a dictionary
my_dict = dict(enumerate(my_list))
print(my_dict)
{0: 'apple', 1: 'banana', 2: 'cherry'}

5. Using fromkeys() method

The fromkeys() method is used to create a new dictionary with keys from a sequence and values set to a value.

It takes two parameters, the first parameter is the sequence of keys and the second parameter is the value to be set for all the keys.

Example
my_list = ["apple", "banana", "cherry"]

# using fromkeys() to create a dictionary
my_dict = dict.fromkeys(my_list, 0)

print(my_dict)
{'apple': 0, 'banana': 0, 'cherry': 0}

Frequency Asked Questions

  1. Can a list have duplicate keys when converted to a dictionary in Python?

    No, a dictionary cannot have duplicate keys. If there are duplicate keys in the list, the last value for that key will be used in the dictionary.

  2. Is it possible to convert a list of lists to a dictionary in Python?

    Yes, it is possible to convert a list of lists to a dictionary in Python using some of the methods discussed in this article, such as the zip() function or a list of tuples.

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

    You can convert a dictionary to a list of tuples using the items() function and then convert the list of tuples to a list using the list() function.

  4. Can I change the order of the keys in a dictionary in Python?

    No, the order of the keys in a dictionary is not guaranteed in Python. If you need to preserve the order of the keys, you can use a collections.OrderedDict.

  5. Is it possible to convert a list to a nested dictionary in Python?

    Yes, it is possible to convert a list to a nested dictionary in Python using some of the methods discussed in this article, such as a list of nested lists or a list of nested tuples.


Conclusion

In this tutorial, we have discussed 5 different ways to conver a list to a dictionary in Python.

Each of these methods has its own advantages and disadvantages. You can choose the method that suits your needs.

Happy Learning! 😇