+ 6
Help me understand this code! Loop do nothing, why list is exchange?
Maybe it is shorted visualisation? If u know full version, write it. https://code.sololearn.com/cQSkaexvc0Tt/?ref=app
11 Answers
+ 5
Wow.... cool stuff
Finally it assigns all elements of the list to a[1]
And the last one wins.
you will see clearly if you replace pass by print(a)
+ 3
The ËforË-loop lets you easily iterate through an iterable like a ËlistË; for that it assigns one element of the iterable in each iteration to the variable or reference you put between ËforË and ËinË, in code you could write it like that:
# Your iterable (the values are not important)
a = [1, 1, 4, 8, 32]
# The ËforË-loop iterates with in iterator:
it = iter(a)
# ... and here you can see the iterations with the assignment:
while True:
try:
# Here is the assignment to your reference between ËforË and ËinË:
a[1] = next(it) # Ënext(it)Ë returns the next element of the iterable (ËËaËË)
except StopIteration:
# The iterator raises a ËStopIterationË exception, when itâs at the end of the iterable.
break
# Here could your code come:
pass
print(a)
# Output: [1, 32, 4, 8, 32]
+ 3
Use the python visualiser (pythontutor.com) to see the step by step of this code.
+ 2
Mike Trofimov no
the list already exists.
Can you tell more?
Where did u find it?
+ 2
check out this link for the full story: https://www.programiz.com/JUMP_LINK__&&__python__&&__JUMP_LINK-programming/iterator
https://code.sololearn.com/cA4jw4h7rkj6/?ref=app
+ 2
áOOáȘ áȘááK
+ 1
this is only my guess
+ 1
I do not give any command to the cycle to change anything in the list. Why list is exchange?
+ 1
Thanks everyone for the answers!
0
If you tried to print inside the loop you might find a patterns.
a = [1, 1, 2, 3, 32]
for a[1] in a:
#pass
print (a)
print(a)
output
[1, 1, 2, 3, 32]
[1, 1, 2, 3, 32]
[1, 2, 2, 3, 32]
[1, 3, 2, 3, 32]
[1, 32, 2, 3, 32]
[1, 32, 2, 3, 32]
- 2
i beat big fat gibber little shit with big nose looking like squidward he be eating too much burgers with some halal sauce that bomber