You are currently viewing Convert List into Dictionary in Python

How to convert a list into a dictionary in Python or create a dictionary from a list? You can use built-in functions like zip() and dict() or more advanced techniques like dictionary comprehensions and loops to convert the list into a Python dictionary. In this article, we will learn how to convert the list into a dict object with examples.

Advertisements

Related: Python Convert Two Lists into Dictionary

1. Quick Examples of Converting List into a Dictionary

These code examples will give you a high-level overview of what we will discuss in this article. If you are in hurry this is a good place to start. You can get an idea of how to convert list into a dictionary. We will discuss each of the examples in detail in later sections.€


# Quick examples of converting a list into a dictionary

# Example 1: Create a dictionary using a dictionary comprehension
my_list = ["Python", "Pandas", "Spark", "PySpark"]  
my_dict = { item : "Course" for item in my_list }  
print(my_dict)  

# Example 2: Convert list to dict using zip() method
def Convert(x):
    iter_obj = iter(x)
    my_dct = dict(zip(iter_obj, iter_obj))
    return my_dct
 list = ['course', 'python', 'fee', 4000, 'duration', '45 days']
print(Convert(list))

# Example 3: Convert list to dict using for loop
def convert(list):
	my_dict = {}
	for i in range(0, len(list), 2):
		my_dict[list[i]] = list[i + 1]
	return my_dict
list = ['course', 'python', 'fee', 4000, 'duration', '45 days']
print(convert(list))

# Example 4: Convert list to dict using enumerate()
my_list = ["Python", "Pandas", "Spark", "PySpark"]  
my_dict = dict(enumerate(my_list, 1))
print(my_dict)

# Example 5: convert list of tuple to dict 
my_list = [("course", "Python"), ("fee", 4000), ("duration", "45 days")]  
my_dict = dict(my_list)
print(my_dict) 

2. Dictionary Comprehension to Convert List to Dict

You can convert list into a dictionary in Python by using dictionary comprehension. A dictionary comprehension is a concise and more pythonic way to create a new dictionary from an iterable by specifying how the keys and values should be mapped to each other.

See the following example.


# Create a dictionary using a dictionary comprehension
my_list = ["Python", "Pandas", "Spark", "PySpark"]  
my_dict = { item : "Course" for item in my_list }  
print(my_dict)  

Yields below output.

Python convert list into dictionary

In the above code, we have created a dictionary using dictionary compression, we converted the list into dictionary in a single line. The list elements are tuned into keys of the dictionary.

Another example, using dictionary comprehension to convert a list to the dictionary.


# convert list to dictionary
def Convert(list):
	my_dict = {list[i]: list[i + 1] for i in range(0, len(list), 2)}
	return my_dict
		
list = ['course', 'python', 'fee', 4000, 'duration', '45 days']
print(Convert(list))

Yields below output.

Python convert list into dictionary

3. Convert List to Dictionary Using zip() function

The zip() function is used to combine the two values which are passed as its arguments. You can use convert() method, to convert one data type to another data type. Create and initialize the iterator and pass it into zip() method, it will zip keys and values together. Finally use typecast to convert it into dict type. 


# Convert list to dict using zip() method
def Convert(x):
    iter_obj = iter(x)
    my_dct = dict(zip(iter_obj, iter_obj))
    return my_dct
 list = ['course', 'python', 'fee', 4000, 'duration', '45 days']
print(Convert(list))

# Output:
# {'course': 'python', 'fee': 4000, 'duration': '45 days'}

4. Convert List to Dictionary Using For Loop

You can also convert the list to dict using for loop. For that we need to create an empty dictionary and then iterate over the list, for each iteration, the key-value pair is added to the dictionary. Finally use typecast to convert it into dict type. 


# Convert list to dict using for loop
def convert(list):
	my_dict = {}
	for i in range(0, len(list), 2):
		my_dict[list[i]] = list[i + 1]
	return my_dict
list = ['course', 'python', 'fee', 4000, 'duration', '45 days']
print(convert(list))

# Output:
# {'course': 'python', 'fee': 4000, 'duration': '45 days'} 

5. Convert List to Dictionary Use enumerate()

You can also use the enumerate() function to add a counter to an iterable object, such as a list, and return an enumerate object. This object contains pairs of the form (index, element) for each element in the iterable.

You can use this method with the help of for loop to create or convert a dictionary from list. See the below python example.


# Convert list to dict using enumerate()
my_list = ["Python", "Pandas", "Spark", "PySpark"]  
my_dict = dict(enumerate(my_list, 1))
print(my_dict)

# Output:
# {1: 'Python', 2: 'Pandas', 3: 'Spark', 4: 'PySpark'}

6. Convert List to Dictionary Using Tuples

Create the list of tuple key/value pairs, and convert it into a dictionary by using the type casting method in Python. It will convert the given list of tuples into a single dictionary. For example,


# convert list of tuple to dict 
my_list = [("course", "Python"), ("fee", 4000), ("duration", "45 days")]  
my_dict = dict(my_list)
print(my_dict)

# Output:
# {'course': 'Python', 'fee': 4000, 'duration': '45 days'}

7. Conclusion

In this article, we have discussed several different methods to convert the list into a python dictionary or create a dictionary from python list. You can use built-in functions like zip() and dict() or more advanced techniques like dictionary comprehensions and loops to convert the list into a Python dictionary.

Happy learning !!