1

I have the following code:

lst = [1,2,3,4]

a = 0
i = 0
for i in lst:
    while a < len(lst):
        a += 1
        print(a, i)

And I'd like it to print

1 1
2 1
3 1
4 1
1 2
2 2
3 2
4 2
1 3
2 3
3 3
4 3
1 4
2 4
3 4
4 4

However, I am struggling to achieve this. I can only get it to print the first item in the list.

1 1
2 1
3 1
4 1

Any help would be appreciated. Thanks

2 Answers 2

3

Move the initialization of a to inside the first loop, so that it gets reset to 0 on every iteration.

lst = [1,2,3,4]

i = 0
for i in lst:
    a = 0
    while a < len(lst):
        a += 1
        print(a, i)
Sign up to request clarification or add additional context in comments.

Comments

2

You forgot to revert a to zero after the while loop is completed

lst = [1,2,3,4]

a = 0
i = 0
for i in last:
    while a < len(last):
        a += 1
        print(a, I)
    a = 0 # <-- add this

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.