Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
zip() | For Loops
Python Loops
course content

Зміст курсу

Python Loops

Python Loops

1. While Loops: Get Started
2. While Loops: Essentials
3. For Loops
4. Nested Loops

zip()

Sometimes you need to work with multiple iterable objects at the same time: for example, process data from two or three lists inside one loop. One of the approaches requires using the zip() function - it is a generator that transfroms the elements of the multiple iterable objects in tuples.

Run the following example to see how it works.

12345
amounts = [4, 1, 6, 5, 12, 2] goods = ['Milk', 'Apple', 'Oatmeal', 'Cucumber', 'Chocolate', 'Potato'] list_ = zip(amounts, goods) list_ = list(list_) # converting generator into list print(list_)
copy

Using zip() in the for loop helps you easy manage the values.

12345
amounts = [4, 1, 6, 5, 12, 2] goods = ['Milk', 'Apple', 'Oatmeal', 'Cucumber', 'Chocolate', 'Potato'] for amount, good in zip(amounts, goods): print(f'Got {amount} kg of {good} in the grocery shop.')
copy

Завдання

left list contains line beginnings of the song and right contains endings. Your task is to output the correct lyrics by concatenating beginnings and endings.

Завдання

left list contains line beginnings of the song and right contains endings. Your task is to output the correct lyrics by concatenating beginnings and endings.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

Секція 3. Розділ 4
toggle bottom row

zip()

Sometimes you need to work with multiple iterable objects at the same time: for example, process data from two or three lists inside one loop. One of the approaches requires using the zip() function - it is a generator that transfroms the elements of the multiple iterable objects in tuples.

Run the following example to see how it works.

12345
amounts = [4, 1, 6, 5, 12, 2] goods = ['Milk', 'Apple', 'Oatmeal', 'Cucumber', 'Chocolate', 'Potato'] list_ = zip(amounts, goods) list_ = list(list_) # converting generator into list print(list_)
copy

Using zip() in the for loop helps you easy manage the values.

12345
amounts = [4, 1, 6, 5, 12, 2] goods = ['Milk', 'Apple', 'Oatmeal', 'Cucumber', 'Chocolate', 'Potato'] for amount, good in zip(amounts, goods): print(f'Got {amount} kg of {good} in the grocery shop.')
copy

Завдання

left list contains line beginnings of the song and right contains endings. Your task is to output the correct lyrics by concatenating beginnings and endings.

Завдання

left list contains line beginnings of the song and right contains endings. Your task is to output the correct lyrics by concatenating beginnings and endings.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

Секція 3. Розділ 4
toggle bottom row

zip()

Sometimes you need to work with multiple iterable objects at the same time: for example, process data from two or three lists inside one loop. One of the approaches requires using the zip() function - it is a generator that transfroms the elements of the multiple iterable objects in tuples.

Run the following example to see how it works.

12345
amounts = [4, 1, 6, 5, 12, 2] goods = ['Milk', 'Apple', 'Oatmeal', 'Cucumber', 'Chocolate', 'Potato'] list_ = zip(amounts, goods) list_ = list(list_) # converting generator into list print(list_)
copy

Using zip() in the for loop helps you easy manage the values.

12345
amounts = [4, 1, 6, 5, 12, 2] goods = ['Milk', 'Apple', 'Oatmeal', 'Cucumber', 'Chocolate', 'Potato'] for amount, good in zip(amounts, goods): print(f'Got {amount} kg of {good} in the grocery shop.')
copy

Завдання

left list contains line beginnings of the song and right contains endings. Your task is to output the correct lyrics by concatenating beginnings and endings.

Завдання

left list contains line beginnings of the song and right contains endings. Your task is to output the correct lyrics by concatenating beginnings and endings.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

Sometimes you need to work with multiple iterable objects at the same time: for example, process data from two or three lists inside one loop. One of the approaches requires using the zip() function - it is a generator that transfroms the elements of the multiple iterable objects in tuples.

Run the following example to see how it works.

12345
amounts = [4, 1, 6, 5, 12, 2] goods = ['Milk', 'Apple', 'Oatmeal', 'Cucumber', 'Chocolate', 'Potato'] list_ = zip(amounts, goods) list_ = list(list_) # converting generator into list print(list_)
copy

Using zip() in the for loop helps you easy manage the values.

12345
amounts = [4, 1, 6, 5, 12, 2] goods = ['Milk', 'Apple', 'Oatmeal', 'Cucumber', 'Chocolate', 'Potato'] for amount, good in zip(amounts, goods): print(f'Got {amount} kg of {good} in the grocery shop.')
copy

Завдання

left list contains line beginnings of the song and right contains endings. Your task is to output the correct lyrics by concatenating beginnings and endings.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 3. Розділ 4
Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
We're sorry to hear that something went wrong. What happened?
some-alt