How to Convert a List to Dictionary in Python?

Posted in /  

How to Convert a List to Dictionary in Python?
vinaykhatri

Vinay Khatri
Last updated on December 21, 2024

    List and Dictionaries are the two most prominent built-in data structures in Python. A list is generally used to collect multiple elements of similar data types, and a dictionary is used to map data (value) with a label (key). In this Python tutorial, we will learn how to convert a list to a dictionary in Python using different methods. Let us start this tutorial by discussing the different ways to convert a list of tuple pairs into a dictionary.

    How to Convert a List of Tuples to Dictionary?

    The data structure of the Python dictionary is quite different from a Python list. A list stores its elements as a single unit and assigns an index number to every element. But in the dictionary, an element is saved as a key-value pair . So, when we want to convert a list into a dictionary, we have to specify both the units, i.e., key and value. To convert a list into a dictionary, we generally need a list of pair tuples. When we convert a list of tuples into a dictionary, the first element of the tuple becomes the key and the second element becomes the value for the key.

    list = [(key1, value1), (key2, value2), (key3, value3)]
    
    to dictionary we will get 
    {key1:value1, key2:value2, key3:value3}

    There are two ways to convert a list of tuples into a dictionary. We can either use the Python dict() function or use dictionary comprehension. Let's discuss both ways. First, we will convert a list of tuples into a dictionary using the dict() function.

    1. using dict() function

    The Python dict() function can accept a list of tuple pairs and convert it into a dictionary. Example

    # list of tuple pairs
    my_list = [("Student1", "Rahul"), ("Student2", "Ravi"), ("Student3", "Aman"), ("Student4", "Vikas")]
    
    # convert the list of tuple pairs into a dictionary
    my_dict = dict(my_list)
    
    print(my_dict)

    Output:

    {'Student1': 'Rahul', 'Student2': 'Ravi', 'Student3': 'Aman', 'Student4': 'Vikas'}
    
    

    The dict() method can only convert a list of tuples or list pairs into a dictionary. It needs two values at the same time so that it can convert them to a key-value pair. If we apply the dict() function on a scalar list, it will throw an error. Example

     >>> my_list = [100, 200, 400, 500]
    >>> dict(my_list)
    Traceback (most recent call last):
    TypeError: cannot convert dictionary update sequence element #0 to a sequence

    2. Using dictionary comprehension

    Dictionary comprehension is a shorthand to create a dictionary using a for loop and an iterable within a single line expression. If we have a list of tuple pairs, we can convert it into a dictionary by using dictionary comprehension. Example

    # list of tuple pairs
    my_list = [("Student1", "Rahul"), ("Student2", "Ravi"), ("Student3", "Aman"), ("Student4", "Vikas")]
    
    # convert the list of tuple pairs into a dictionary
    my_dict = {key:value for key, value in my_list}
    
    print(my_dict)

    Output:

    {'Student1': 'Rahul', 'Student2': 'Ravi', 'Student3': 'Aman', 'Student4': 'Vikas'}
    

    How to Convert a Python List into Dictionary?

    Let us assume that we have a scalar list, and we want to convert that list into a dictionary where the list index becomes the key and the element becomes the value for the dictionary. Example

    our list
    [200, 300, 400, 500, 600, 700]
    to dictionary
    {0:200, 1:300, 2:400, 3:500, 4:600, 5:700}

    To perform such a conversion, we can take the help of the Python zip function. The zip function accepts two iterable objects and returns a zip iterator of tuples. Example

    >>> it1 = ["a", "b", "c", "d"]
    >>> it2 = [1, 2, 3, 4]
    >>> my_list = list(zip(it1, it2))
    >>> my_list
    [('a', 1), ('b', 2), ('c', 3), ('d', 4)]

    The zip() function returns a zip iterator, so we use the list function to convert the zip iterator to a list of tuple pairs. Now, let's use the zip function with dict() and dictionary comprehension and convert a scalar Python list to a dictionary. Example: Using dict() function

    # main list
    values = ["A", "B", "C", "D", "E"]
    
    # list of keys
    keys = range(len(values))
    
    # list of tuple (key,value) pairs
    my_list = list(zip(keys, values))
    
    # convert the list of tuple pairs into a dictionary
    my_dict = dict(my_list)
    
    print(my_dict)

    Output:

    {0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E'}

    Example: Using dictionary comprehension

    # main list
    values = ["A", "B", "C", "D", "E"]
    
    # list of keys
    keys = range(len(values))
    
    # list of tuple (key,value) pairs
    my_list = list(zip(keys, values))
    
    # convert the list of tuple pairs into a dictionary
    my_dict = {key:value for key, value in my_list}
    
    print(my_dict)

    Output:

    {0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E'}
    

    Conclusion

    In this Python tutorial, we discussed how to convert a Python list into a Python dictionary. To convert a list into a dictionary, we can either use the dict() function or dictionary comprehension. The most essential thing to keep in mind is that the list must be a collection of tuple pairs so the newly created dictionary could have data for both keys and values. If you have any doubts or suggestions, feel free to share them as comments below. People are also reading:

    Leave a Comment on this Post

    0 Comments