user5661083
user5661083

Reputation: 107

Python: Printing two lists in a for loop

I have two lists.

one=["first","second"]
two=[1,2]
for o in one:
    for t in two:
        print(o)
        print(t)

and the output is:

first
1
first
2
second
1
second
2

Here is what the correct output should be:

first
1
2
second
1
2

There one more ,my professor has asked to print the output-

first
1
second
2

Upvotes: 0

Views: 422

Answers (4)

Will
Will

Reputation: 4469

I think this is what you want:

for o in one:
    print(o)
    for t in two:
        print(t)

So that you only print the string number once per time you run the inner loop.

EDIT:

you can do this:

both = zip(one, two)
for tup in both:
  for val in tup:
    print(val, end=' ')

Upvotes: 3

Mark Skelton
Mark Skelton

Reputation: 3891

Your code should be this:

one=["first","second"]
two=[1,2]
for o in one:
    print(o)
    for t in two:
        print(t)

Upvotes: 2

nathan.medz
nathan.medz

Reputation: 1603

Just a slight change.

    one=["first","second"]
    two=[1,2]
    for o in one:
        print(o)
        for t in two:
            print(t)

Upvotes: 2

Ian Price
Ian Price

Reputation: 7626

for o in one:
    print(o)
    for t in two:
        print(t)

Upvotes: 2

Related Questions