Iterate two list with the same number of elements at once | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Iterate two list with the same number of elements at once

I am trying to figure out how to make those 2 for loops in just one. I tried to put together both for loops, but the output is a tuple, so it doesn`t work for me. And I am stucked in tryong to build a function: for i in first: if len(i) > 4: print("Error: Numbers cannot be more than four digits.") elif i.isalpha(): print("Error: Numbers must only contain digits.") else: if len(i) > maxlenf: maxlenf = len(i) for i in second: maxlens=0 if len(i) > 4: print("Error: Numbers cannot be more than four digits.") elif i.isalpha(): print("Error: Numbers must only contain digits.") else: if len(i) > maxlens: maxlens = len(i) Somebody can help, please.

26th Nov 2020, 5:37 PM
Goyo Dominguez
Goyo Dominguez - avatar
4 Answers
+ 2
Please give sample data...because from what I can work out...your lists are lists of numbers as strings e.g:- first = ["100", "200","300", "400"] and not... first = [100, 200, 300, 400] <== len and isalpha on each element will not work with this.
26th Nov 2020, 6:51 PM
rodwynnejones
rodwynnejones - avatar
+ 7
have you tried: for i, j in zip(first, second) ?
26th Nov 2020, 5:50 PM
Angelo
Angelo - avatar
+ 2
The zip version mentioned by Angelo is nice. Why do you think it is a problem? i is the element of first, j is the element of second list. In each iteration do the checks on i and j. Maybe initialize the maxlen counters before the loop? Or is that on purpose? Another way could be something like this: for idx in range(0, len(first)): # do sth with first[idx] # do sth with second[idx]
26th Nov 2020, 7:44 PM
Lisa
Lisa - avatar
0
yes, but the output is a tuple, so I don’t know how to process it afterwards...😩
26th Nov 2020, 6:37 PM
Goyo Dominguez
Goyo Dominguez - avatar