DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

zip in Python

Buy Me a Coffee

*Memos:

zip() can create the iterable which has zero or more iterables as shown below:
*Memos:

  • The 1st or more arguments are *iterables(Optional-Type:iterable). *Don't use any keyword like *iterables, iterables, iterable, etc.
  • The iterable stops when the shortest input iterable is exhausted.
  • The iterable cannot be directly accessed with index so use list() to access it with index.
fruits =     ["Apple",   "Orange", "Banana", "Kiwi",    "Lemon", "Mango"]
meats =      ["Chicken", "Beef",   "Pork",   "Duck",    "Mutton"]
vegetables = ["Onion",   "Carrot", "Garlic", "Spinach", "Eggplant"]

print(zip())
# <zip object at 0x7eace0bead00>

print(zip(fruits, meats, vegetables))
# <zip object at 0x7da5876aaa40>

print(list(zip(fruits, meats, vegetables)))
# [('Apple', 'Chicken', 'Onion'),
#  ('Orange', 'Beef', 'Carrot'),
#  ('Banana', 'Pork', 'Garlic'),
#  ('Kiwi', 'Duck', 'Spinach'),
#  ('Lemon', 'Mutton', 'Eggplant')]

print(list(zip(fruits, meats, vegetables))[0])
# ('Apple', 'Chicken', 'Onion')

print(zip(fruits, meats, vegetables)[0])
# Error

f, m, v = list(zip(fruits, meats, vegetables))[0]
print(f, m, v)
# Apple Chicken Onion

for f, m, v in zip(fruits, meats, vegetables):
    print(f, m, v)
# Apple Chicken Onion
# Orange Beef Carrot
# Banana Pork Garlic
# Kiwi Duck Spinach
# Lemon Mutton Eggplant
Enter fullscreen mode Exit fullscreen mode
fruits =     ["Apple",   "Orange", "Banana", "Kiwi",    "Lemon", "Mango"]
meats =      ["Chicken", "Beef",   "Pork",   "Duck",    "Mutton"]
vegetables = ["Onion",   "Carrot", "Garlic", "Spinach", "Eggplant"]

print(list(zip(zip(fruits, meats), vegetables)))
# [(('Apple', 'Chicken'), 'Onion'),
#  (('Orange', 'Beef'), 'Carrot'),
#  (('Banana', 'Pork'), 'Garlic'),
#  (('Kiwi', 'Duck'), 'Spinach'),
#  (('Lemon', 'Mutton'), 'Eggplant')]

print(list(zip(zip(fruits, meats), vegetables))[0])
# (('Apple', 'Chicken'), 'Onion')

fm, v = list(zip(zip(fruits, meats), vegetables))[0]
print(fm, v)
# ('Apple', 'Chicken') Onion

(f, m), v = list(zip(zip(fruits, meats), vegetables))[0]
[f, m], v = list(zip(zip(fruits, meats), vegetables))[0]
print(f, m, v)
# Apple Chicken Onion

for fm, v in zip(zip(fruits, meats), vegetables):
    print(fm, v)
# ('Apple', 'Chicken') Onion
# ('Orange', 'Beef') Carrot
# ('Banana', 'Pork') Garlic
# ('Kiwi', 'Duck') Spinach
# ('Lemon', 'Mutton') Eggplant

for (f, m), v in zip(zip(fruits, meats), vegetables):
for [f, m], v in zip(zip(fruits, meats), vegetables):
    print(f, m, v)
# Apple Chicken Onion
# Orange Beef Carrot
# Banana Pork Garlic
# Kiwi Duck Spinach
# Lemon Mutton Eggplant
Enter fullscreen mode Exit fullscreen mode
fruits =     ["Apple",   "Orange", "Banana", "Kiwi",    "Lemon", "Mango"]
meats =      ["Chicken", "Beef",   "Pork",   "Duck",    "Mutton"]
vegetables = ["Onion",   "Carrot", "Garlic", "Spinach", "Eggplant"]

print(list(zip(zip(fruits, zip(meats)), vegetables)))
# [(('Apple', ('Chicken',)), 'Onion'),
#  (('Orange', ('Beef',)), 'Carrot'),
#  (('Banana', ('Pork',)), 'Garlic'),
#  (('Kiwi', ('Duck',)), 'Spinach'),
#  (('Lemon', ('Mutton',)), 'Eggplant')]

print(list(zip(zip(fruits, zip(meats)), vegetables))[0])
# (('Apple', ('Chicken',)), 'Onion')

fm, v = list(zip(zip(fruits, zip(meats)), vegetables))[0]
print(fm, v)
# ('Apple', ('Chicken',)) Onion

(f, m), v = list(zip(zip(fruits, zip(meats)), vegetables))[0]
[f, m], v = list(zip(zip(fruits, zip(meats)), vegetables))[0]
print(f, m, v)
# Apple ('Chicken',) Onion

(f, (m,)), v = list(zip(zip(fruits, zip(meats)), vegetables))[0]
[f, [m]], v = list(zip(zip(fruits, zip(meats)), vegetables))[0]
print(f, m, v)
# Apple Chicken Onion

for fm, v in zip(zip(fruits, zip(meats)), vegetables):
    print(fm, v)
# ('Apple', ('Chicken',)) Onion
# ('Orange', ('Beef',)) Carrot
# ('Banana', ('Pork',)) Garlic
# ('Kiwi', ('Duck',)) Spinach
# ('Lemon', ('Mutton',)) Eggplant

for (f, m), v in zip(zip(fruits, zip(meats)), vegetables):
for [f, m], v in zip(zip(fruits, zip(meats)), vegetables):
    print(f, m, v)
# Apple ('Chicken',) Onion
# Orange ('Beef',) Carrot
# Banana ('Pork',) Garlic
# Kiwi ('Duck',) Spinach
# Lemon ('Mutton',) Eggplant

for (f, (m,)), v in zip(zip(fruits, zip(meats)), vegetables):
for [f, [m]], v in zip(zip(fruits, zip(meats)), vegetables):
    print(f, m, v)
# Apple Chicken Onion
# Orange Beef Carrot
# Banana Pork Garlic
# Kiwi Duck Spinach
# Lemon Mutton Eggplant
Enter fullscreen mode Exit fullscreen mode
fruits = ["Apple", "Orange", "Banana", "Kiwi", "Lemon", "Mango"]

print(list(enumerate(zip(range(-6, 8, 3), fruits), start=-2)))
# [(-2, (-6, 'Apple')),
#  (-1, (-3, 'Orange')),
#  (0, (0, 'Banana')),
#  (1, (3, 'Kiwi')),
#  (2, (6, 'Lemon'))]

print(list(enumerate(zip(range(-6, 8, 3), fruits), start=-2))[0])
# (-2, (-6, 'Apple'))

i, jf = list(enumerate(zip(range(-6, 8, 3), fruits), start=-2))[0]
print(i, jf)
# -2 (-6, 'Apple')

i, (j, f) = list(enumerate(zip(range(-6, 8, 3), fruits), start=-2))[0]
i, [j, f] = list(enumerate(zip(range(-6, 8, 3), fruits), start=-2))[0]
print(i, j, f)
# -2 -6 Apple

for i, jf in enumerate(zip(range(-6, 8, 3), fruits), start=-2):
    print(i, jf)
# -2 (-6, 'Apple')
# -1 (-3, 'Orange')
# 0 (0, 'Banana')
# 1 (3, 'Kiwi')
# 2 (6, 'Lemon')

for i, (j, f) in enumerate(zip(range(-6, 8, 3), fruits), start=-2):
for i, [j, f] in enumerate(zip(range(-6, 8, 3), fruits), start=-2):
    print(i, j, f)
# -2 -6 Apple
# -1 -3 Orange
# 0 0 Banana
# 1 3 Kiwi
# 2 6 Lemon
Enter fullscreen mode Exit fullscreen mode

Top comments (0)