Zipping
Zipping means attaching two different iterables to create a new one that contains values from both.
This is very convenient when you have multiple tracks of values that should proceed concurrently. Imagine you had names and surnames and you want to just get a list of people:
names = [ 'Sam', 'Axel', 'Aerith' ] surnames = [ 'Fisher', 'Foley', 'Gainsborough' ]
How to do it...
We want to zip together names and surnames:
>>> people = zip(names, surnames)
>>> list(people)
[('Sam', 'Fisher'), ('Axel', 'Foley'), ('Aerith', 'Gainsborough')]
How it works...
Zip will make a new iterable where each item in the newly-created iterable is a collection that is made by picking one item for each one of the provided iterables.
So, result[0] = (i[0], j[0])
, and result[1] = (i[1], j[1])
, and so on. If i
and j
have different lengths, it will stop as soon as one of the two is exhausted.
If you want to proceed until you exhaust the longest one of the provided iterables instead of stopping on the...