In Python, we can zip two lists together easily with the zip() function. You can create new lists of tuples or dictionaries with the zip() function.
list1 = ["this","is","a","list"]
list2 = [1, 2, 3, 4]
print(list(zip(list1, list2)))
print(dict(zip(list1, list2)))
#Output:
[('this', 1), ('is', 2), ('a', 3), ('list', 4)]
{'this': 1, 'is': 2, 'a': 3, 'list': 4}
When working with collections of data in Python, the ability to manipulate them and create new collections is very valuable.
One such manipulation is the ability to zip multiple lists into a new list or dictionary in Python.
The Python zip() function returns a zip object, which is an iterator of tuples where each item from each list is paired together.
If the passed iterable objects have different lengths, the iterator with the least items decides the length of the new iterator.
We can use the zip() function to zip two lists and create a list of tuples easily in Python.
Below is a simple example in Python of using zip() to zip two lists and creating a new list.
list1 = ["this","is","a","list"]
list2 = [1, 2, 3, 4]
print(list(zip(list1, list2)))
#Output:
[('this', 1), ('is', 2), ('a', 3), ('list', 4)]
Creating Different Collections When Zipping Two Lists in Python
The Python zip function returns a zip object, which is an iterator of tuples. We can convert a returned zip object into a number of different objects.
For example, we can easily create a new list from a zip object after zipping two lists together with the list() function.
Below is how to create a new list with the zip() function after zipping two lists.
list1 = ["this","is","a","list"]
list2 = [1, 2, 3, 4]
print(list(zip(list1, list2)))
#Output:
[('this', 1), ('is', 2), ('a', 3), ('list', 4)]
We can also create dictionaries after zipping two lists.
Let’s say we have a list of keys and a list of values. We can create a dictionary easily by using the dict() function to convert the zip object to a dictionary object.
Below is an example in Python of how to create a dictionary from a zip object.
keys = ["this","is","a","list"]
values = [1, 2, 3, 4]
print(dict(zip(keys, values )))
#Output:
{'this': 1, 'is': 2, 'a': 3, 'list': 4}
Zipping Two Lists of Unequal Length in Python
If you want to zip two lists which don’t have the same length, you should be aware that the zip() function will remove values from the longer list.
For example, if you have a list of three and five items and try to zip them together, you will get a zip object with three items.
list1 = ["apple", "banana", "cherry"]
list2 = [1, 2, 3, 4, 5]
print(list(zip(list1, list2)))
#Output:
[('apple', 1), ('banana', 2), ('cherry', 3)]
If you want to zip based on the list with the longer length, you can use the itertools module zip_longest() function.
zip_longest() zips two lists and for the items where we cannot pair values, because we don’t have a value from the shorter list, zip_longest() fills in None.
list1 = ["apple", "banana", "cherry"]
list2 = [1, 2, 3, 4, 5]
print(list(zip(list1, list2)))
#Output:
[('apple', 1), ('banana', 2), ('cherry', 3), (None, 4), (None, 5)]
If you’d prefer to fill in a value other than None, you can use the “fillvalue” argument to have zip_longest() fill a different value than None.
For example, we can fill the None values with an empty string instead of None as shown in the following Python code.
list1 = ["apple", "banana", "cherry"]
list2 = [1, 2, 3, 4, 5]
print(list(zip(list1, list2, fillvalue='')))
#Output:
[('apple', 1), ('banana', 2), ('cherry', 3), ('', 4), ('', 5)]
Hopefully this article has been useful for you to learn how to zip two lists in Python with zip().