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.
12345amounts = [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_)
Using zip()
in the for
loop helps you easy manage the values.
12345amounts = [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.')
Swipe to start coding
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.
Lösung
Danke für Ihr Feedback!
single
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen
Zusammenfassen Sie dieses Kapitel
Code in file erklären
Erklären, warum file die Aufgabe nicht löst
Awesome!
Completion rate improved to 6.25
zip()
Swipe um das Menü anzuzeigen
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.
12345amounts = [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_)
Using zip()
in the for
loop helps you easy manage the values.
12345amounts = [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.')
Swipe to start coding
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.
Lösung
Danke für Ihr Feedback!
Awesome!
Completion rate improved to 6.25single