Python zip() function
Walk several sequences side by side.
Syntax
zip(*iterables, strict=False)
Gives tuples of the first items, then the second items, and so on. Stops at the shortest sequence, unless strict=True, which raises an error when lengths differ.
Parameters
| *iterables | two or more sequences |
| strict | error if the lengths differ |
Return value
a zip object (an iterator of tuples)
Example
Code
names = ["Sita", "Leo", "Mia"] scores = [82, 91, 77] for name, score in zip(names, scores): print(name, score) print(list(zip([1, 2, 3], "ab"))) # stops at the shorter one print(dict(zip(names, scores)))
Output
Sita 82
Leo 91
Mia 77
[(1, 'a'), (2, 'b')]
{'Sita': 82, 'Leo': 91, 'Mia': 77}
Related
Learn it step by step in the free lesson Loops, with exercises that check your answers.