How to Use zip() Function in PythonPython Basics


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

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

A = [1,2,3]
B = [4,5,6]
C = [4,5,6]
zipped = zip(A,B,C)
print(zipped)
print( list(zipped))

Output:

[(1, 4, 4), (2, 5, 5), (3, 6, 6)]

Syntax

zip(iterator, ...)

Parameters

Name Description
iterator One or more iterators.

Return Value

It returns a zip object.

© 2024 Cheerlearn.com, all rights reserved. Privacy Policy | Contact Us