Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate over two lists and recycle the second one until the first one is over

Basically I have these 2 lists

listone = ['a', 'b', 'c', 'd', 'e', 'f', 'j']
listtwo = ['1', '2', '3']

And I want to iterate over both of the lists synchronously and whenever the shortest list ends (listtwo in this case) is restarts again until listone is finished. Example:

a 1
b 2
c 3
d 1
e 2
f 3
j 1

Like this.

like image 584
Leo Avatar asked Feb 08 '20 12:02

Leo


People also ask

Can you do a for loop for two lists?

We can iterate over a single Python list using a for loop or a range() function. The range(start, stop, step) function enables us to get a sequence of numbers from a defined range of values.

How do you print two lists in Python?

You can use the zip() function to join lists together. The zip() function will iterate tuples with the corresponding elements from each of the lists, which you can then format as Michael Butscher suggested in the comments. Finally, just join() them together with newlines and you have the string you want.

How to iterate through two or more lists in Python?

To iterate through 2 or more different lists can be done using 2 functions, they are To use zip function we must import the itertools module. Importing this module is as same as any other module in python. Zip () function must be used when the user wants to stop printing after anyone of the lists completes iterating.

What is the difference between iterating over single List and multiple lists?

Iterating over single lists, refers to using for loops for iteration over a single element of a single list at a particular step whereas in iterating over multiple lists simultaneously, we refer using for loops for iteration over a single element of multiple lists at a particular step.

What is the best way to loop over a list?

Best for… One of the simplest ways to loop over a list in Python is by using a for loop. A for loop allows you to iterate over an interable object (like a list) and perform a given action. This approach is intuitive because it loops over each item in the list (unless explicitly stopped).

How do I iterate through a list of iterable elements?

Make an iterator that returns elements from the first iterable until it is exhausted, then proceeds to the next iterable, until all of the iterables are exhausted. If you have your lists in a list, itertools.chain.from_iterable is available: l = [l1, l2] for i in itertools.chain.from_iterable (l): print (i, end=" ") Which yields the same result.


Video Answer


3 Answers

you can use itertools.cycle:

from itertools import cycle

for i, j in zip(listone, cycle(listtwo)):
    print(i, j)

output:

a 1
b 2
c 3
d 1
e 2
f 3
j 1
like image 78
kederrac Avatar answered Oct 18 '22 22:10

kederrac


Without itertools.cycle() or creating secondary list:

listone = ['a', 'b', 'c', 'd', 'e', 'f', 'j']
listtwo = ['1', '2', '3']
N = len(listtwo)

for index, elem in enumerate(listone):
    elem2 = listtwo[index % N]
    print(elem, elem2)
like image 41
Ajit Panigrahi Avatar answered Oct 19 '22 00:10

Ajit Panigrahi


from math import ceil
listone = ['a', 'b', 'c', 'd', 'e', 'f', 'j']
listtwo = ['1', '2', '3']
listtwo_augmented = listtwo * ceil(len(listone)/len(listtwo))
for e1, e2 in zip(listone, listtwo_augmented):
    print(e1, e2)
like image 40
abdullah.cu Avatar answered Oct 18 '22 23:10

abdullah.cu