Convert List to Dictionary in Python

How to Convert List to Dictionary in Python

Convert List to Dictionary in Python

In Python, a dictionary is an unordered collection of mutable and indexed key-value pairs, whereas a list is an ordered and mutable collection of elements. Converting a list to a dictionary can be useful in various scenarios, such as organizing data efficiently or performing quick lookups. In this article, we’ll explore different methods to achieve this conversion in Python.

Complete Python Course with Advance topics:-Click here

Method 1: Using a Loop

Using a loop is one of the simplest methods for turning a list into a dictionary. This approach allows you to define the keys and values explicitly.

Example:

# List of fruits  
fruits = ['apple', 'banana', 'cherry']  
  
# Initialize an empty dictionary  
fruit_dict = {}  
  
# Populate the dictionary using a loop  
for idx, fruit in enumerate(fruits):  
    fruit_dict[idx] = fruit  
  
print(fruit_dict)

Output:

{0: 'apple', 1: 'banana', 2: 'cherry'}

In this example, the enumerate() function helps keep track of the index while iterating over the list, assigning each fruit to a corresponding numeric key.

Method 2: Using zip()

The zip() function provides an efficient way to combine multiple lists into key-value pairs, making it a quick method to convert lists to dictionaries.

Example:

# List of fruits and their prices  
fruits = ['apple', 'banana', 'cherry']  
prices = [1.00, 0.50, 1.50]  
  
# Convert lists to a dictionary using zip()  
fruit_dict = dict(zip(fruits, prices))  
  
print(fruit_dict)

Output:

{'apple': 1.0, 'banana': 0.5, 'cherry': 1.5}

Here, the zip() function pairs elements from both lists to create key-value pairs, where fruits act as keys and prices as values.

Method 3: Using Dictionary Comprehension

Python’s dictionary comprehension provides a concise and elegant way to convert a list to a dictionary, particularly when you need to apply transformations on elements.

Example:

# List of fruits  
fruits = ['apple', 'banana', 'cherry']  
  
# Convert list to a dictionary using dictionary comprehension  
fruit_dict = {fruit: len(fruit) for fruit in fruits}  
  
print(fruit_dict)

Output:

{'apple': 5, 'banana': 6, 'cherry': 6}

Dictionary comprehension generates key-value pairs in this example, where each fruit serves as a key and the fruit name’s length serves as its corresponding value.

Download New Real Time Projects :-Click here
Complete Advance AI topics:- CLICK HERE

Conclusion

Converting a list to a dictionary in Python can be done in multiple ways, depending on the desired key-value structure and the complexity of operations. Whether you choose a loop, zip(), or dictionary comprehension, each method offers flexibility and efficiency for different use cases.

Stay tuned to UpdateGadh for more Python tutorials and programming insights!


how to convert list to dictionary in python using for loop
convert list to dictionary python with index as key
convert list to dictionary keys python
how to convert list to dictionary in python w3schools
python create dictionary from list of keys and values
convert  list to dictionary in python
convert list of dictionaries to dictionary python
convert string to dictionary python
convert list to dictionary in python using for loop
convert list to dictionary in python for loop
Convert List to Dictionary in Python

Post Comment