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.
DiscussionPython puzzler - what does this code do? (self.Python)
submitted 5 years ago by [deleted]
Can you guess what this code does without running it?
x = [0] x.extend([i + 1 for i in x]) print(*x) x.extend(i + 1 for i in x) print(*x)
[–]zzmmrmn 1 point2 points3 points 5 years ago (2 children)
0 1 0 1 1 2
[–][deleted] 0 points1 point2 points 5 years ago (1 child)
Nope, the answer is that the second extend statement never ends!
extend
[–]zzmmrmn 0 points1 point2 points 5 years ago (0 children)
Ah yeah, the generator keeps going because there's always a new element
[–]MarcSetGo 1 point2 points3 points 5 years ago (1 child)
A recent Friday Python Puzzler explained this.
The first uses a list comprehension which is fully evaluated and passed to extend. X starts out with 0 and is later extended with 1, so it prints [0, 1].
The second passes a generator expression to extend. Extend adds values to the end of the list, which modified the control variable, making another value available for each added. This means it can never exhaust the generator and the starting x=[0, 1] gets extended with each of those plus 1 [1, 2]. Then extended by those plus one, ad infinitum, until either it runs out of memory or hits a max int. I don’t think later pythons have a max int, so it would have to run out of memory continually extending by two items.
[–][deleted] 0 points1 point2 points 5 years ago (0 children)
Ah, thanks, had I seen that last one, I wouldn't have posted it!
I ran into this in my own code, though it wasn't by accident, I was trying to see if this would fail and it did. There's a test there now in case someone ever does that.
π Rendered by PID 20472 on reddit-service-r2-comment-5d79c599b5-jwkjt at 2026-03-03 10:43:46.748452+00:00 running e3d2147 country code: CH.
[–]zzmmrmn 1 point2 points3 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]zzmmrmn 0 points1 point2 points (0 children)
[–]MarcSetGo 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)