Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python nested for loop when index of outer loop equals to length of inner loop you start again

I have

A = ['A','B','C','D','E','F','G','H']
B= ['a','b']

want to iterate through the list to get

C = ['Aa','Bb','Ca','Db','Ea','Fb','Ga','Hb']

how can I go about it in python

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can do this by using the index in the A list mapped on the number of elements in the B list with the modulo operation (%) :

A = [1,2,3,4,5,6,7,8]
B= ['a','b']
C = []

for i in range(len(A)):
    C.append(f"{A[i]}{B[i%2]}")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading