C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Tuple: In the for-loop we unpack each element from zip's return value. So item1 and item2 are from each respective list.
ListForPython program that uses zip on list
items1 = ["blue", "red", "green", "white"]
items2 = ["sky", "sunset", "lawn", "pillow"]
# Zip the two lists and access pairs together.
for item1, item2 in zip(items1, items2):
print(item1, "...", item2)
Output
blue ... sky
red ... sunset
green ... lawn
white ... pillow
Python program that uses zip, unequal lengths
list1 = [10, 20, 30, 40]
list2 = [11, 21, 31]
# Zip the two lists.
result = zip(list1, list2)
# The zipped result is only 3 elements long.
# ... The fourth element of list1 was discarded.
for element1, element2 in result:
print(element1, element2)
Output
10 11
20 21
30 31
Python program that shows zip object
list1 = [0]
list2 = [1]
# The result is a zip object.
result = zip(list1, list2)
print(result)
Output
<zip object at 0x02884120>
TypeError: We cause TypeErrors when we try to use unavailable functions on zip objects. Please see the "convert to list" example.
Python program that causes subscript error
list1 = [0, 1, 2]
list2 = [3, 4, 5]
# Zip together the two lists.
zipped = zip(list1, list2)
# This causes an error.
print(zipped[0])
Output
Traceback (most recent call last):
File "C:\programs\file.py", line 12, in <module>
print(zipped[0])
TypeError: 'zip' object is not subscriptable
Python program that causes len error
zipped = zip([1, 2], [3, 4])
# This causes an error.
print(len(zipped))
Output
Traceback (most recent call last):
File "C:\programs\file.py", line 7, in <module>
print(len(zipped))
TypeError: 'zip' has no length
Python program that converts zip object to list
# Zip up these two lists.
zipped = zip([1, 2], [3, 4])
# Convert the zip object into a list.
result_list = list(zipped)
print(result_list)
Output
[(1, 3), (2, 4)]