Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Dictionary creation using list contents in Python
Changing the collection type from one type to another is a very frequent need in Python. In this article we will see how we create a dictionary when multiple lists are given. The challenge is to combine all these lists to create one dictionary accommodating all these values in a dictionary key-value format.
Using zip() Function
The zip() function can be used to combine the values of different lists. In the below example we have taken three lists as input and combine them to form a single dictionary. One of the lists supplies the keys for the dictionary and the other two lists hold the value fields for each of the key ?
Example
key_list = [1, 2, 3]
day_list = ['Friday', 'Saturday', 'Sunday']
fruit_list = ['Apple', 'Banana', 'Grape']
# Given Lists
print("Given key list : " + str(key_list))
print("Given day list : " + str(day_list))
print("Given fruit list : " + str(fruit_list))
# Dictionary creation
res = {key: {'Day': day, 'Fruit': fruit} for key, day, fruit in
zip(key_list, day_list, fruit_list)}
# Result
print("The final dictionary : \n", res)
The output of the above code is ?
Given key list : [1, 2, 3]
Given day list : ['Friday', 'Saturday', 'Sunday']
Given fruit list : ['Apple', 'Banana', 'Grape']
The final dictionary :
{1: {'Day': 'Friday', 'Fruit': 'Apple'}, 2: {'Day': 'Saturday', 'Fruit': 'Banana'}, 3: {'Day': 'Sunday', 'Fruit': 'Grape'}}
Using enumerate() Function
The enumerate() function adds a counter as the key of the enumerate object. In our case, we will use the index to access corresponding values from other lists ?
Example
key_list = [1, 2, 3]
day_list = ['Friday', 'Saturday', 'Sunday']
fruit_list = ['Apple', 'Banana', 'Grape']
# Given Lists
print("Given key list : " + str(key_list))
print("Given day list : " + str(day_list))
print("Given fruit list : " + str(fruit_list))
# Dictionary creation
res = {val : {"Day": day_list[index], "Fruit": fruit_list[index]}
for index, val in enumerate(key_list)}
# Result
print("The final dictionary : \n", res)
The output of the above code is ?
Given key list : [1, 2, 3]
Given day list : ['Friday', 'Saturday', 'Sunday']
Given fruit list : ['Apple', 'Banana', 'Grape']
The final dictionary :
{1: {'Day': 'Friday', 'Fruit': 'Apple'}, 2: {'Day': 'Saturday', 'Fruit': 'Banana'}, 3: {'Day': 'Sunday', 'Fruit': 'Grape'}}
Simple Dictionary from Two Lists
For a basic key-value pair dictionary from two lists, you can use zip() with the dict() constructor ?
Example
keys = ['name', 'age', 'city']
values = ['John', 25, 'New York']
# Create dictionary
result = dict(zip(keys, values))
print("Dictionary from two lists:", result)
The output of the above code is ?
Dictionary from two lists: {'name': 'John', 'age': 25, 'city': 'New York'}
Comparison
| Method | Best For | Complexity |
|---|---|---|
zip() |
Multiple lists of same length | Simple and readable |
enumerate() |
When you need index access | More control over indexing |
dict(zip()) |
Simple key-value pairs | Most concise |
Conclusion
Use zip() for combining multiple lists into complex dictionary structures. Use enumerate() when you need index-based access, and dict(zip()) for simple key-value pairs from two lists.
