Python zip() Function
Example
Concatenate two tuples:
a = ("Bill", "Steve", "Elon") b = ("Gates", "Jobs", "Musk") x = zip(a, b)
Definition and Usage
The zip() function returns a zip object, which is an iterator of tuples, where the first items from each passed iterator are paired together, then the second items from each passed iterator are paired together, and so on.
If the passed iterators have different lengths, the length of the new iterator will be determined by the iterator with the fewest items.
Syntax
zip(iterator1, iterator2, iterator3 ...)
Parameter Value
Parameter | Description |
---|---|
iterator1, iterator2, iterator3 ... | Concatenated iterator objects. |
More Examples
Example
If a tuple contains more items, these items will be ignored:
a = ("Bill", "Steve", "Elon") b = ("Gates", "Jobs", "Musk", "Richard") x = zip(a, b)