Skip to main content
deleted 123 characters in body
Source Link
viraptor
  • 34.4k
  • 13
  • 116
  • 204

For exemple, say i have the following code:

a = [[1],[2],[3]]
for c in a: 
    print(c)
    for d in a: 
        print(d)

The current output of it is:

[1]
[1]
[2]
[3]
[2]
[1]
[2]
[3]
[3]
[1]
[2]
[3]

My question is weatherwhether it is possible to modify this code in a pretty way so that the output becamesbecomes like this:

[1]
[2]
[3]
[2]
[3]
[3]

In other words if it is possible, in a 'clean python', to have the inner loop to cover the list from the index the outer loop stopped.

I am new to python, first learned programming in c. I can only think of dumb ways to do it, so could do with some help.

For exemple, say i have the following code:

a = [[1],[2],[3]]
for c in a: 
    print(c)
    for d in a: 
        print(d)

The current output of it is:

[1]
[1]
[2]
[3]
[2]
[1]
[2]
[3]
[3]
[1]
[2]
[3]

My question is weather it is possible to modify this code in a pretty way so that the output becames like this:

[1]
[2]
[3]
[2]
[3]
[3]

In other words if it is possible, in a 'clean python', to have the inner loop to cover the list from the index the outer loop stopped.

I am new to python, first learned programming in c. I can only think of dumb ways to do it, so could do with some help.

For exemple, say i have the following code:

a = [[1],[2],[3]]
for c in a: 
    print(c)
    for d in a: 
        print(d)

The current output of it is:

[1]
[1]
[2]
[3]
[2]
[1]
[2]
[3]
[3]
[1]
[2]
[3]

My question is whether it is possible to modify this code in a pretty way so that the output becomes like this:

[1]
[2]
[3]
[2]
[3]
[3]

In other words if it is possible, in a 'clean python', to have the inner loop to cover the list from the index the outer loop stopped.

Source Link

Is it possible to have a python inner 'for' to continue where the outer for stopped?

For exemple, say i have the following code:

a = [[1],[2],[3]]
for c in a: 
    print(c)
    for d in a: 
        print(d)

The current output of it is:

[1]
[1]
[2]
[3]
[2]
[1]
[2]
[3]
[3]
[1]
[2]
[3]

My question is weather it is possible to modify this code in a pretty way so that the output becames like this:

[1]
[2]
[3]
[2]
[3]
[3]

In other words if it is possible, in a 'clean python', to have the inner loop to cover the list from the index the outer loop stopped.

I am new to python, first learned programming in c. I can only think of dumb ways to do it, so could do with some help.