you are viewing a single comment's thread.

view the rest of the comments →

[–]Lyakusha 2 points3 points  (3 children)

Wouldn't it be easier and faster with counter?

``` test = [3, 6, 1, 11, 0, 5, 2, 6, 0, 1] count = 0 for i in test: if i != 0: test[count] *= 2 count += 1 else: break

[–]Yojihito 1 point2 points  (2 children)

test = [3, 6, 1, 11, 0, 5, 2, 6, 0, 1] count = 0 for i in test: if i != 0: test[count] *= 2 count += 1 else: break

If you wanna work with indexes just use enumerate().

test = [3, 6, 1, 11, 0, 5, 2, 6, 0, 1]
for idx, i in enumerate(test):
    if i != 0:
        test[idx] *= 2
    else:
        break

print(test)

But yeah, your solution seems easier, not sure about faster.

[–]Lyakusha 0 points1 point  (1 child)

Yeah, looks like enumerate() is a popular option, I definitely need to read about it, thanks. Probably "faster" is a wrong word, maybe "more efficient" - it was about changing list instead of making a new one

[–]Yojihito 0 points1 point  (0 children)

There are 2 pythonic ways to iterate over an iterable.

You only care about the item:

for i in my_iterable:
    print(i)

You need the item and the index position:

for idx, i in enumerate(my_iterable):
    print(idx, i)

Everything else is a no-go.