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
How to zip a Python Dictionary and List together?
In this article, we will show you how to zip a Python dictionary and list together. Below are the various methods to accomplish this task ?
Using zip() function
Using append() function and items() function
Using append() function and in operator
Using zip() Function
The zip() function combines two iterables by pairing their elements. When zipping a dictionary with a list, we use items() to get key-value pairs from the dictionary ?
# input dictionary
input_dict = {'Hello': 1, 'tutorialspoint': 2, 'python': 3, 'codes': 3}
# input list
input_list = [10, 20, 30, 40]
# combining input dictionary and list together
zipped_result = zip(input_dict.items(), input_list)
print("Combining input dictionary and list together:")
# printing the resultant zip result by converting the
# above zip object into a list
print(list(zipped_result))
Combining input dictionary and list together:
[(('Hello', 1), 10), (('tutorialspoint', 2), 20), (('python', 3), 30), (('codes', 3), 40)]
Accessing Zip Object Elements
You can iterate through the zip object to access individual elements ?
# input dictionary
input_dict = {'Hello': 1, 'tutorialspoint': 2, 'python': 3, 'codes': 3}
# input list
input_list = [10, 20, 30, 40]
# combining input dictionary and list together
zipped_object = zip(input_dict.items(), input_list)
print("Zipping input dictionary and list:")
# traversing through key, value pair of dictionary and list items in
# the above zip object
for (k, v), list_item in zipped_object:
# printing the corresponding key of a dictionary
print(f"Key: {k}")
# printing the corresponding value of a dictionary
print(f"Value: {v}")
# printing corresponding list item of the input list
print(f"List item: {list_item}")
print("---")
Zipping input dictionary and list: Key: Hello Value: 1 List item: 10 --- Key: tutorialspoint Value: 2 List item: 20 --- Key: python Value: 3 List item: 30 --- Key: codes Value: 3 List item: 40 ---
Using append() and items() Functions
This approach manually creates the zipped structure using a loop and append() ?
# input dictionary
input_dict = {'Hello': 1, 'tutorialspoint': 2, 'python': 3, 'codes': 3}
# input list
input_list = [10, 20, 30, 40]
# empty list for storing result
result = []
# initializing a variable of list index with 0
i = 0
# traversing through key, value pair of input dictionary
for key, value in input_dict.items():
# creating a tuple of key-value pairs of a dictionary
dict_key_value_tuple = (key, value)
# appending corresponding key, value pair of input dictionary,
# and input list element to the above result list
result.append((dict_key_value_tuple, input_list[i]))
# incrementing the value of the list index by 1
i += 1
# printing the resultant zip of input dictionary and list
print("Zipping input dictionary and list:")
print(result)
Zipping input dictionary and list:
[(('Hello', 1), 10), (('tutorialspoint', 2), 20), (('python', 3), 30), (('codes', 3), 40)]
Using append() Function and in Operator
This method iterates through dictionary keys using the in operator instead of items() ?
# input dictionary
input_dict = {'Hello': 1, 'tutorialspoint': 2, 'python': 3, 'codes': 3}
# input list
input_list = [10, 20, 30, 40]
# empty list for storing result
result = []
# initializing a variable of list index with 0
i = 0
# traversing through key of input dictionary
for key in input_dict:
# creating a tuple of key-value pair of a dictionary
# here input_dict[key] represents value of a dictionary
dict_key_value_tuple = (key, input_dict[key])
# appending corresponding key, value pair of input dictionary,
# input list element as tuple to the above result list
result.append((dict_key_value_tuple, input_list[i]))
# incrementing the value of the list index by 1
i += 1
# printing the resultant zip of input dictionary and list
print("Zipping input dictionary and list:")
print(result)
Zipping input dictionary and list:
[(('Hello', 1), 10), (('tutorialspoint', 2), 20), (('python', 3), 30), (('codes', 3), 40)]
Comparison
| Method | Code Complexity | Memory Usage | Best For |
|---|---|---|---|
zip() |
Simple | Efficient (iterator) | Most situations |
append() + items() |
Manual | Creates list immediately | Custom processing needed |
append() + in |
Manual | Creates list immediately | When avoiding items() |
Conclusion
The zip() function is the most efficient and pythonic way to combine dictionaries and lists. Use manual approaches with append() only when you need custom processing during the zipping operation.
