Python Convert List to Dictionary

Here are six ways to convert a list to a dictionary in Python: 

  1. Using Dictionary Comprehension
  2. Using dict()
  3. Using map() and and lambda
  4. Using dict.fromKeys()
  5. Using a for loop
  6. Using zip() function

Method 1: Using Dictionary Comprehension

The easiest way to create a dictionary is to use dictionary comprehension, particularly if you have two separate lists: one for keys and another for values.

It iterates over the indices of the keys list. For each iteration, it takes an element from the keys list as the key and the corresponding element from the values list as the value.

Visual Representation

Visual Representation of Python Convert List to Dictionary using Dictionary Comprehension

Example

# Define two lists: one for keys and one for values
ott_keys = ['Disney', 'Netflix', 'Hulu']
ott_values = [10, 20, 30]

# Use dictionary comprehension to create a dictionary from the lists
ott_dict = {ott_keys [i]: ott_values[i] for i in range(len(ott_keys))}

print("Dictionary:",ott_dict)
print(type(ott_dict))

Output

Dictionary: {'Disney': 10, 'Netflix': 20, 'Hulu': 30}
<class 'dict'>

Method 2: Using dict()

If you have a list of tuples, where each tuple contains two elements (key, value), you can directly convert it into a dictionary using dict().

Similarly, if you have a list of lists, where each inner list contains two elements (key, value), you can use this method.

Visual Representation

Visual Representation of Using dict()

Example

list_of_tuples = [('Disney', 10), ('Netflix', 20), ('Hulu', 30)]

ott_dict = dict(list_of_tuples)

print("Dictionary:",ott_dict)
print(type(ott_dict))

Output

Dictionary: {'Disney': 10, 'Netflix': 20, 'Hulu': 30}
<class 'dict'>

Method 3: Using map() and lambda function

The range() function generates a sequence of numbers, starting from 0 up to the length of the list (exclusive), incrementing by 2. This means it creates indices for every alternate element in the list.

The map() function then applies the lambda function to each of these indices, which creates a tuple by pairing the element at the current index with its subsequent element.

These tuples are then converted into a dictionary using the dict() function.

Visual Representation

Visual Representation of Using map() and lambda function

Example

# Example list with alternating keys and values
ott_list = ['Disney', 10, 'Netflix', 20, 'Hulu', 30]

# Use map to pair each key with its corresponding value
paired = map(lambda i: (ott_list[i], ott_list[i + 1]), range(0, len(ott_list), 2))

# Convert the paired tuples into a dictionary
ott_dict = dict(paired)

print(ott_dict)
print(type(ott_dict))

Output

{'Disney': 10, 'Netflix': 20, 'Hulu': 30}
<class 'dict'>

Method 4: Using dict.fromKeys()

If you have a list of keys and want to assign the same value to each key, you can use the fromkeys() method.

Visual Representation

Visual Representation of Using dict.fromKeys()

Example

# Define a list of keys for the dictionary
ott_keys = ['Disney', 'Netflix', 'Hulu']

# Define a common value to be assigned to all keys
ott_values = 10

# Use dict.fromkeys() to create a dictionary
ott_dict = dict.fromkeys(ott_keys, ott_values)

print("Dictionary:", ott_dict)
print("Data type of dictionary:", type(ott_dict))

Output

Dictionary: {'Disney': 10, 'Netflix': 10, 'Hulu': 10}
Data type of dictionary: <class 'dict'>

Method 5: Using a for loop

The enumerate function is used in the for loop to get both the key and value(index) of each element in the list. 

Example

ott_keys = ['Disney', 'Netflix', 'Hulu']

# Initialize an empty dictionary
ott_dict = {}

# Loop through the list and use each element as a key and its index as the value
for index, key in enumerate(ott_keys):
 ott_dict[key] = index

print(ott_dict)

Output

{'Disney': 0, 'Netflix': 1, 'Hulu': 2}
<class 'dict'>

Method 6: Using zip() function

The zip() function takes two lists and creates a list of tuples, where each tuple consists of a key-value pair.

Then, it converts the zipped pairs into a dictionary using the dict() function.

Visual Representation

Visual Representation of Using zip() function with dict()

Example

ott_keys = ['Disney', 'Netflix', 'Hulu']
ott_values = [10, 20, 30]

ott_dict = dict(zip(ott_keys, ott_values))

print("Dictionary:",ott_dict)
print(type(ott_dict))

Output

Dictionary: {'Disney': 10, 'Netflix': 20, 'Hulu': 30}
<class 'dict'>

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.