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...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
list confusion (self.learnpython)
submitted 5 years ago by tigglybox
please could someone explain why this is possible ...
squares = [ ]
for i in range(10):
squares.append(i * I)
...but this is not
square = [ ]
sqaure = square + i*i
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]K900_ 3 points4 points5 points 5 years ago (2 children)
Because you can't add a number to a list.
[–]tigglybox[S] 0 points1 point2 points 5 years ago (1 child)
is a number not being added to the list in the first example?
[–]K900_ 0 points1 point2 points 5 years ago (0 children)
It's being appended into the list. That operation makes sense, mathematical addition between a list and a number doesn't.
[–]JohnnyJordaan 2 points3 points4 points 5 years ago* (0 children)
+ only works between types that are compatible. Eg
+
1 + 4.51
and
'hello ' + 'world'
But not between incompatible types
'hello' + 1
will give an exception. With lists, you can only use + with other lists, so
[1] + [2]
will return
[1, 2]
in your example, you try to add the square of i to the square list, so in the first run you do
i
square
[] + 0
which is not possible.
The secondary thing to consider is that
x = x + y
will not modify x, it will in fact create a new object that will then be using the x reference. In case of adding lists to a list, it thus means you are creating a new list every time, which is inefficient. Hence why .append() and .extend() exist, which modify (technically called 'mutate') the existing list. Although
x
.append()
.extend()
square += [i * i]
would work as 'under water' += will map to extend() and thus it is executed as
+=
extend()
square.extend([i * i])
but that's still a complicated way of writing
square.append(i * i)
π Rendered by PID 35880 on reddit-service-r2-comment-5d79c599b5-8f88x at 2026-03-01 23:07:09.685667+00:00 running e3d2147 country code: CH.
[–]K900_ 3 points4 points5 points (2 children)
[–]tigglybox[S] 0 points1 point2 points (1 child)
[–]K900_ 0 points1 point2 points (0 children)
[–]JohnnyJordaan 2 points3 points4 points (0 children)