+ 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

4th Feb 2020, 6:28 PM
Mike Trofimov
Mike Trofimov - avatar
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)
4th Feb 2020, 6:32 PM
Oma Falk
Oma Falk - avatar
+ 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]
5th Feb 2020, 10:08 AM
Greif
+ 3
Use the python visualiser (pythontutor.com) to see the step by step of this code.
5th Feb 2020, 11:36 PM
Chuks AJ
Chuks AJ - avatar
+ 2
Mike Trofimov no the list already exists. Can you tell more? Where did u find it?
5th Feb 2020, 6:03 AM
Oma Falk
Oma Falk - avatar
+ 2
ᘜOOá—Ș á’ȘᑌᑕK
6th Feb 2020, 11:57 AM
Ilsaf
Ilsaf - avatar
+ 1
this is only my guess
5th Feb 2020, 9:21 AM
Mike Trofimov
Mike Trofimov - avatar
+ 1
I do not give any command to the cycle to change anything in the list. Why list is exchange?
5th Feb 2020, 9:24 AM
Mike Trofimov
Mike Trofimov - avatar
+ 1
Thanks everyone for the answers!
5th Feb 2020, 1:53 PM
Mike Trofimov
Mike Trofimov - avatar
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]
6th Feb 2020, 7:38 AM
Stanley K Ludovick
Stanley K Ludovick - avatar
- 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
7th Feb 2020, 9:38 AM
code nootnoot
code nootnoot - avatar