J.Doe
J.Doe

Reputation: 27

How do I loop through 2 arrays?

I have 2 different arrays and I need help printing both of them

Route = ["Bus A","Bus B","Bus C","Bus D","Bus E","Bus F"]
DaysLate = [ [1],[2],[3],[4],[5],[6] ]

is there a way I can get this output?

Bus A 1
Bus B 2
Bus C 3
Bus D 4
Bus E 5
Bus F 6

Upvotes: 1

Views: 68

Answers (3)

Mykola Zotko
Mykola Zotko

Reputation: 17794

You can also use the function chain.from_iterable() from itertools module to chain all sublists into a single sequence:

for i, j in zip(Route, itertools.chain.from_iterable(DaysLate)):
    print(i, j)

Alternatively you can use a star * to unpack sublists:

for i, j in zip(Route, DaysLate):
    print(i, *j)

Upvotes: 0

Heyran.rs
Heyran.rs

Reputation: 513

Try this:

for i in range(6):
    print(Route[i], DaysLate[i][0])

Upvotes: 0

Mehrdad Pedramfar
Mehrdad Pedramfar

Reputation: 11073

Try this:

Route = ["Bus A","Bus B","Bus C","Bus D","Bus E","Bus F"]
DaysLate = [ [1],[2],[3],[4],[5],[6] ]

for i,j in zip(Route,DaysLate):
    print(i, j[0])

Upvotes: 2

Related Questions