Python Convert List Pairs to a Dictionary

The Python dictionary is a structure used to store data. This data type is appropriate when the data needs to contain labels (unique keys) associated with a value (key:value). Python lists are not able to handle this format.  The list(s) will require conversion to a dictionary format (key:value).

In the code samples below, there are various ways to accomplish this conversion.

Convert List Pairs of Tuples to a Dictionary

To successfully convert a list pair of tuples into a dictionary (key:value) format, each tuple in the list must contain exactly two elements. The first element of each tuple represents a unique dictionary key. The second element of the tuple represents a dictionary value, thus creating a key:value pair.

Line [2] uses the dict() method and passes poets_list as a parameter.  This line creates and adds a key:value pair for each tuple encountered. The output from this conversion displays below.

poets_lst  = [('Frost', 1874), ('Keats', 1795), 
              ('Emerson', 1803), ('Burns', 1759)]
poets_dict = dict(poets_lst)
print(poets_dict)

Output

{'Frost': 1874, 'Keats': 1795, 'Emerson': 1803, 'Burns': 1759}

Feel free to check out our related video about the dict() built-in function:

Convert List Pairs of Tuples to a Dictionary using Dictionary Comprehension

Another way to convert a list of tuples into a dictionary (key:value) format is to use the dictionary comprehension method. As noted above, the first element of each tuple represents a unique dictionary key. The second element of the tuple represents a dictionary value, thus creating a key:value pair.

Line [2] creates the poets_dict dictionary. The dictionary comprehension method loops through each tuple, creating a key:value pair and adding to poets_dict. The output from this conversion displays below.

poets_lst   = [('Frost', 1874), ('Keats', 1795), 
               ('Emerson', 1803), ('Burns', 1759)]
poets_dict = {k:v for k, v in poets_lst}
print(poets_dict)

Output

{'Frost': 1874, 'Keats': 1795, 'Emerson': 1803, 'Burns': 1759}

Feel free to learn about dictionary comprehension in our related video tutorial:

Convert List Pairs to a Dictionary using dict() and zip()

In this example, there are two lists of equal lengths (p_names and p_born). The first list contains the names of poets (p_names) and becomes the unique dictionary keys. The second list is the birth year (p_born) value associated with the key.

Example

Key           Value	             Key:Value   
p_names[0]    p_born[0] =  'Frost', 1874
p_names[1]    p_born[1] =  'Keats', 1795, etc.

On line [3], the dict() method calls the zip() method that’s easily explained in a graphic:

Python zip() - Visual Example

In the following code examples, the two lists serve as the parameters for these methods. The first parameter will be the unique keys (p_names). The second parameter will be the values for said keys. The output from this conversion displays below.

p_names = ['Frost', 'Keats', 'Emerson', 'Burns']
p_born = [1874, 1795, 1803, 1759]
poets = dict(zip(p_names, p_born))
print(poets)

Output

{'Frost': 1874, 'Keats': 1795, 'Emerson': 1803, 'Burns': 1759}

Convert List Pairs to a Dictionary using fromkeys()

The fromkeys() method creates a new dictionary with the option to pass two parameters. The first parameter is a sequence of elements (such as a list). These elements convert to unique keys. The second parameter is optional. This parameter can be assigned a single value or no value at all. If the second parameter is empty, the default value for each key:value pair is None.

For this example, the list (p_keys) assigns unique keys for each key:value pair. Notice below that p_value contains an individual value. Each value in the key:value pair resolves to this string.

p_keys = ['Frost', 'Keats', 'Emerson', 'Burns']
p_value = 'Poet'
poet_dict = dict.fromkeys(p_keys, p_value)
print(poet_dict)

Output:

{'Frost': 'Poet', 'Keats': 'Poet', 'Emerson': 'Poet', 'Burns': 'Poet'}

Related Video – Convert Two Lists to Dict

Do you want to join our free email academy? Sign up here: