In last week's snippet post we talked about the incredibly powerful
zip function, and how we can use it to make our loops more Pythonic. This week we're going to be talking about the less well-known brother of
zip_longest lives in the
itertools module, which we've spoken about briefly before.
itertools contains all kinds of useful functions revolving around iterative operations.
So how does
zip_longest differ from plain old
zip? Why should we care about it?
Well, when we use
zip will stop combining our iterables as soon as one of them runs out of elements. If the other iterables are longer, we just throw those excess items away. Take a look at this example:
l_1 = [1, 2, 3] l_2 = [1, 2] combinated = list(zip(l_1, l_2)) print(combinated) # [(1, 1), (2, 2)]
As you can see, we just lost that
l_1. That can sometimes be pretty problematic. We don't generally want to be throwing away data like this.
Luckily we have
zip_longest here to save us.
Let's look at our example above again. This time using
from itertools import zip_longest l_1 = [1, 2, 3] l_2 = [1, 2] combinated = list(zip_longest(l_1, l_2, fillvalue="_")) print(combinated) # [(1, 1), (2, 2), (3, '_')]
There are a few things to note here. For one, we got a third tuple in our zip object, meaning that the longer list was able to provide all of its values. Secondly, we have this keyword argument called
If we look at the
3 to our
In essence, any time
zip_longest doesn't have a value to match to one of our iterable's elements, it will place this
fillvalue there to plug the gaps.
We can really use any
fillvalue we want here: numbers, lists, dictionaries,
None. Whatever you can think of. This makes it incredibly versatile, and it's definitely a good one to know about.
In case you're interested, we can call
zip_longest without a
fillvalue argument, in which case it will default to using
None as a
That's it for this snippet post! I hope you learnt something new, and I'm sure you'll find all sorts of awesome ways to use
zip_longest in your own code.
We're still offering our Complete Python Course for just $9.99 when you use the coupon code
BLOGGER, so if you're interested in upgrading your Python skills, come check us out. There's a 30 day money back guarantee, so you really have nothing to lose.