zip() by passing in the sequences you want to operate on. And you get back a generator. That’s great from a memory management perspective but not particularly instructive. So let me do that again, converting the generator into a list.
zip() on this corrupted data is problematic. It just does it. This can be a hard bug to find. You end up with a zipped sequence with just one less item in it. This is even worse if the missing piece of data is in the middle.
There is an alternative to this that’s been kicking around for a while in the
itertools module. It’s called
zip_longest(). It works like
zip() but inserts
None into the data when there’s a length mismatch. Let’s run it on the Lego data.
Here, I’ve set
0, and you can see the difference at the end of the sequence in the New York City tuple. Appropriately enough, the lesson on
zip() was rather zippy. Next up, a miscellaneous, catchall, collage, grab bag, hodgepodge, potpourri smorgasbord of the smaller stuff that’s left in 3.10. Why, I do own a thesaurus! Why do you ask?
Become a Member to join the conversation.