Skip to content

Python Zip Lists – Zip Two or More Lists in Python

Python Zip Lists Cover Image

In this post, you’ll learn how to use Python to zip lists, including how to zip two or more lists in Python. Specifically, you’ll learn how to zip two Python lists, zip three or more lists, lists of different lengths, and lists of lists.

What is the Python zip function?

The Python zip() function is a built-in function that returns an iterator object containing tuples, each of which contain a series of typles containing the elements from each iterable object.

Now, this definition is a little complex. It can be helpful to think of the zip() function as combining two or more lists (or other iterable objects) into an object containing ordered tuples from the lists.

For example, you’re given two lists: list_a, which contains [1,2,3,4] and list_b, which contains ['a', 'b', 'c', 'd']. If you were to zip these two lists, you’d get back the following:

# Understanding the Python zip() function

list_a = [1,2,3,4]
list_b = ['a', 'b', 'c', 'd']

print(list(zip(list_a, list_b)))

# Returns: [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]

Now, you may notice that we’ve wrapped the zip() function with the list() function. The reason for this is that the zip function actually returns a zip object. We can confirm this by checking the type of this:

# What is a zip object?

list_a = [1,2,3,4]
list_b = ['a', 'b', 'c', 'd']

zipped = zip(list_a, list_b)

print(zipped)

# Returns: <zip object at 0x00000163B0D53FC0>

The zip function takes in any number of iterable objects and will zip them together. However, something to keep in mind: the built-in zip function will max out at the length of the shortest iterable provided. Because of this, if one list contains 5 elements and another list contains a million – the resulting zip object will just contain 5 items. Later on in this post, we’ll explore how to work with zipping lists of different lengths.

How to Zip Two Lists in Python

Zipping two lists in Python is a very easy thing to do. The function takes care of most things, without much user input. Essentially, the zip() function will accept any number of interables.

In the example below, you’ll learn how to zip two lists in:

list_a = [1,2,3,4]
list_b = ['a', 'b', 'c', 'd']

zipped = zip(list_a, list_b)
zipped_list = list(zipped)

print(zipped_list)

# Returns: [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]

As shown in the example above, to acces to zipped lists, we need to find ways of unpacking that list. We’ve done this by converting it to a list, using the list() function.

We can also do this with a for-loop:

list_a = [1,2,3,4]
list_b = ['a', 'b', 'c', 'd']

zipped = zip(list_a, list_b)

for item in zipped:
    print(item)

This returns:

(1, 'a')
(2, 'b')
(3, 'c')
(4, 'd')

In this section you learned how to zip two lists and also how to access its items.

How to Zip 3 (or more) Lists in Python

The Python zip() function makes it easy to also zip more than two lists. This works exactly like you’d expect, meaning you just only need to pass in the lists as different arguments.

In the example below, you’ll learn how to zip three lists and then return it as a list of tuples, using the built-in list() function:

list_a = [1,2,3,4]
list_b = ['a', 'b', 'c', 'd']
list_c = [99, 88, 77, 66]

zipped = zip(list_a, list_b, list_c)
zipped_list = list(zipped)

print(zipped_list)

# Returns: [(1, 'a', 99), (2, 'b', 88), (3, 'c', 77), (4, 'd', 66)]

Here you have learned how to zip three (or more) lists in Python, using the built-in zip() function! Next, you’ll learn how to work with lists of different lengths.

How to Zip Lists of Different Lengths in Python

Zipping lists of unequal or different lengths results in a zip object that is as long as the shortest iterable in the items being passed in. This means, that if you pass in a list that is 5 items long, and another list that is one million items long, you’ll end up with a zip item that contains five items.

Let’s verify this by zipping two lists of different lengths:

list_short = list(range(5))
list_long = list(range(1000000))

zipped = zip(list_short, list_long)
zipped_list = list(zipped)

print(zipped_list)

# Returns: [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]

Now, this may not be what you want to happen. For this reason, the built-in itertools package comes with a .zip_longest() function.

Let’s see what the .zip_longest() function looks like:

itertools.zip_longest(*iterables, fillvalue=Nona)

The function takes any number of iterables as well as an optional fillvalue. Any value that doesn’t have a corresponding zip item gets filled with this fillvalue.

Let’s see how this works in practice. We won’t work with a list that’s a million items, but perhaps something a bit more reasonable:

from itertools import zip_longest

numbers = [1,2,3,4,5]
names = ['nik', 'datagy', 'james']

zipped = zip_longest(numbers, names, fillvalue='missing')
zipped_list = list(zipped)

print(zipped_list)

# Returns: [(1, 'nik'), (2, 'datagy'), (3, 'james'), (4, 'missing'), (5, 'missing')]

You have now learned how to work with lists of different lengths and be able to properly zip them.

How to Zip Lists of Lists in Python

Finally, let’s take a look at how to zip lists of lists. Say you’re given a list of lists that contains two lists: list_of_lists = [[1,2,3,4],['a', 'b', 'c', 'd']] and you want to turn it into the following zipped list: [(1, 'a'), (2, 'b') ...].

Python comes with a handy way to unpack objects. This can be very helpful when you don’t know how many items an object has. Let’s see how this works in practice:

list_of_lists = [[1,2,3,4],['a', 'b', 'c', 'd']]
zipped = zip(*list_of_lists)
zipped_list = list(zipped)
print(zipped_list)

# Returns: [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]

In the example above, Python:

  • First unpacks each of the item of the outer list, thereby returning the two inner lists.
  • Then, these lists are then zipped.
  • Finally, the zip object is turned into a list.

Conclusion

In this post, you learned how to zip two or more Python lists. You also learned how to work with lists of different (unequal) lengths as well as with lists of lists.

To learn more about the Python zip() function, check out the official documentation here. To learn more about itertool’s zip_longest() function, check out the official documentation here.

Additional Resources

To learn more about related topics, check out the tutorials below:

Nik Piepenbreier

Nik is the author of datagy.io and has over a decade of experience working with data analytics, data science, and Python. He specializes in teaching developers how to use Python for data science using hands-on tutorials.View Author posts

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *