Python zip() Function
Python zip() Function
The zip() function makes an iterator that aggregates elements from each of the iterables and returns an iterator of tuples.
Syntax
zip(*iterables)
Parameter
iterables: Iterator objects that will be joined together
Return
This function returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables.
Example 1
# Python program explaining
# the zip() function
# initializing lists
name = [ "Reema", "Meehu", "Manvi", "Rani" ]
id_no = [ 164, 121, 323, 212 ]
marks = [ 40, 50, 60, 70 ]
# using zip() function to map values
mapped = zip(name, id_no, marks)
# converting values to print as set
mapped = set(mapped)
# printing the resultant values
print ("The zipped result is : ",end="")
print (mapped)
Output
The zipped result is : {('Meehu', 121, 50), ('Reema', 164, 40), ('Rani', 212, 70), ('Manvi', 323, 60)}
Example 2
# Python program explaining
# the zip() function
str1 = ("Reema", "Sukla","Varun")
str2 = ("Anjali","Amar", "Monica")
zip_val = zip(str1, str2)
#use the tuple() function to display a readable version of the result:
print(tuple(zip_val))
Output
(('Reema', 'Anjali'), ('Sukla', 'Amar'), ('Varun', 'Monica'))
