Python’s zip() function

numbers = list(range(5)) # list of numbers 
letters = ['a', 'b', 'c', 'd', 'e'] # list of numbers
# Create an iterator object using zip()
zipped = zip(numbers, letters)
# Print the type of zipped
print('Type of zipped is: {}'.format(type(zipped)))
Type of zipped is: <class 'zip'>
# Create a list of tuples from zipped object
zipped_list = list(zipped)
# Print the list of tuples
print('List of tupples is: {}'.format(zipped_list))
List of tupples is: [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e')]
for num, lett in zip(numbers, letters): 
print(num, lett)
0 a
1 b
2 c
3 d
4 e
pairs = [(0,'x'), (1, 'y'), (2, 'z')] # list of tupples# use unpacking operator * to unzip the data
nums, alphabets = zip(*pairs)
# Print the outputs
(0, 1, 2)
('x', 'y', 'z')



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dr. Soumen Atta, Ph.D.

Dr. Soumen Atta, Ph.D.

Postdoctoral Researcher at Laboratoire des Sciences du Numérique de Nantes (LS2N), Université de Nantes, IMT Atlantique, Nantes, France.