Home > Net >  How to count 2 lists in parallel in Python?
How to count 2 lists in parallel in Python?

Time:03-31

I have two lists.

Numbers = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
Letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']

I want to count these two lists in parallel. When the next value is selected in one list, the next value is selected in another list too.

Like this:

Number: 1, Letter: a
Number: 2, Letter: b
Number: 3, Letter: c
Number: 4, Letter: d
Number: 5, Letter: e
Number: 6, Letter: f

and so on. How can I do this? Thanks in advance.

CodePudding user response:

Pythonic way

for x,y in  zip(Numbers,Letters ):
    print(f"Number{x},Letter{y}")

CodePudding user response:

for i in range(len(Numbers)):
    print("Number: "   Numbers[i]   ", Letter: "   Letters[i])

Outputs:

Number: 1, Letter: a
Number: 2, Letter: b
Number: 3, Letter: c
Number: 4, Letter: d
Number: 5, Letter: e
Number: 6, Letter: f
Number: 7, Letter: g
Number: 8, Letter: h
Number: 9, Letter: i
Number: 10, Letter: j

CodePudding user response:

i = 0
while i <len(Numbers):
    print(Numbers[i], Letters[i])
    i =1
  • Related