zip()#
The zip()
function is used to take iterables as arguments, pack the corresponding elements from each object into tuples, and then return an object composed of these tuples.
One application of this is to iterate through multiple arrays simultaneously:
If the arrays are of unequal length, the shortest length will be used.
If you want to use the longest length, you can use another function called zip_longest()
, but I won't go into detail here.
enumerate()#
The enumerate()
function is used to combine a traversable data object (such as a list, tuple, or string) into an indexed sequence, while listing both the data and the index.
This can also be used during iteration.
Notes#
When using these two functions, you need to import the itertools
module, otherwise an error will occur.