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).

There are various ways to accomplish this conversion in the code samples below.


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.

poets_lst  = [('Frost', 1874), ('Keats', 1795),  ('Emerson', 1803), ('Burns', 1759)]
poets_dict = dict(poets_lst)
print(poets_dict)
  • Line [1] creates a list of tuples containing poet names and date of births. This saves to poets_list.
  • 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.
  • Line [3] outputs poets_dict to the terminal.

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.

poets_lst   = [('Frost', 1874), ('Keats', 1795), ('Emerson', 1803), ('Burns', 1759)]
poets_dict = {k:v for k, v in poets_lst}
print(poets_dict)
  • Line [1] creates a list of tuples containing poet names and date of births. This saves to poets_list.
  • 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.
  • Line [3] outputs poets_dict to the terminal.

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).

πŸ’‘Β Note: The first list element (p_names[0]) contains the name of the poet (p_names) and becomes the unique dictionary key. The second element (p_born[0]) becomes the dictionary value for the key.

Example

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

Python zip() - Visual Example

The two lists serve as parameters for these methods in the following code examples.

p_names = ['Frost', 'Keats', 'Emerson', 'Burns']
p_born = [1874, 1795, 1803, 1759]
poets = dict(zip(p_names, p_born))
print(poets)
  • Line [1] creates a list of Poet Names and saves to p_names.
  • Line [2] creates a list of Poet Birth Years and saves to p_born.
  • LIne [3] uses dict() and zip() to pass the lists as parameters. The result saves to poets.
  • Line [4] outputs poets to the terminal.

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.

πŸ’‘Β Note: 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: