Python zip() Function

Example

Concatenate two tuples:

a = ("Bill", "Steve", "Elon")
b = ("Gates", "Jobs", "Musk")
x = zip(a, b)

Run Instance

Definition and Usage

The zip() function returns a zip object, which is an iterator of tuples, where the first items of each passed iterator are paired together, then the second items of each passed iterator are paired together, and so on.

If the passed iterators have different lengths, the number of items in the iterator with the shortest length will determine the length of the new iterator.

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)

Run Instance