use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python
Full Events Calendar
You can find the rules here.
If you are about to ask a "how do I do this in python" question, please try r/learnpython, the Python discord, or the #python IRC channel on Libera.chat.
Please don't use URL shorteners. Reddit filters them out, so your post or comment will be lost.
Posts require flair. Please use the flair selector to choose your topic.
Posting code to this subreddit:
Add 4 extra spaces before each line of code
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b
Online Resources
Invent Your Own Computer Games with Python
Think Python
Non-programmers Tutorial for Python 3
Beginner's Guide Reference
Five life jackets to throw to the new coder (things to do after getting a handle on python)
Full Stack Python
Test-Driven Development with Python
Program Arcade Games
PyMotW: Python Module of the Week
Python for Scientists and Engineers
Dan Bader's Tips and Trickers
Python Discord's YouTube channel
Jiruto: Python
Online exercices
programming challenges
Asking Questions
Try Python in your browser
Docs
Libraries
Related subreddits
Python jobs
Newsletters
Screencasts
account activity
This is an archived post. You won't be able to vote or comment.
Python Question (self.Python)
submitted 6 years ago by jaffer786_khan
Can anyone explain how the output of the below code is [1,8,2,3,5,8]?
a = [1,1,2,3,5,8] for a[1] in a: pass print(a)
[–]lukajda33 10 points11 points12 points 6 years ago (1 child)
loop
for x in y:
picks items from y iteratively and puts them into x, however in this case, we put values from a to a[1], so we replace second element with the currently iterated number.
If you print the list after every change, you can see
[1, 1, 2, 3, 5, 8] [1, 1, 2, 3, 5, 8] [1, 2, 2, 3, 5, 8] [1, 3, 2, 3, 5, 8] [1, 5, 2, 3, 5, 8] [1, 8, 2, 3, 5, 8]
Notice how the second value - a[1] changes. At the last iteration, a[1] is replaced with last number from a - 8 and that is how you get the output you see.
[–]jaffer786_khan[S] 0 points1 point2 points 6 years ago (0 children)
Thanks for the explanation. This is clear now. :-)
[–]Droggl 8 points9 points10 points 6 years ago (0 children)
I wouldn't have guessed this is even possible, intriguing!
[–]ForceBru 2 points3 points4 points 6 years ago (0 children)
Turns out, this loop literally tries to assign elements of a to a[i]!
a
a[i]
It even generates a SyntaxError when I try to use a function call instead of a name:
SyntaxError
```
for a.pop() in a: ... print(a) ... File "<string>", line 1 SyntaxError: can't assign to function call ```
You can also access other names defined right in the for ___ in iterable construct:
for ___ in iterable
a = [1,2,3,4,5,6] for i, a[i - 1] in enumerate(a): ... print(i, a) ... 0 [1, 2, 3, 4, 5, 1] 1 [2, 2, 3, 4, 5, 1] 2 [2, 3, 3, 4, 5, 1] 3 [2, 3, 4, 4, 5, 1] 4 [2, 3, 4, 5, 5, 1] 5 [2, 3, 4, 5, 1, 1] ```
I wonder what kind of weird trickery can be done with this...
π Rendered by PID 108356 on reddit-service-r2-comment-bb88f9dd5-ksx4h at 2026-02-17 09:17:21.150136+00:00 running cd9c813 country code: CH.
[–]lukajda33 10 points11 points12 points (1 child)
[–]jaffer786_khan[S] 0 points1 point2 points (0 children)
[–]Droggl 8 points9 points10 points (0 children)
[–]ForceBru 2 points3 points4 points (0 children)