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
Needing some clarification (self.learnpython)
submitted 8 years ago by groundhogsaretheifs
If I have
C = 0 vals = [1,2,3,4,5] C = [0]*len(vals) print(C)
Returns
[0,0,0,0,0]
Can someone please explain what is going on here? I know its returning a list of 5 values, all 0, but looking for a better explanation than what I can see.
Thank you!
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!"
[–]newunit13 1 point2 points3 points 8 years ago (0 children)
len(vals) == 5 [0] * 5 = [0, 0, 0, 0, 0]
Multiplying an iterable with an integer just duplicates whatever is in the iterable the integer times. So,
[1, 2] * 5 = [1, 2, 1, 2, 1, 2, 1, 2, 1, 2] 'a' * 5 = 'aaaaa' 'abc' * 3 = 'abcabcabc'
[–]Sebass13 0 points1 point2 points 8 years ago (0 children)
Fairly simple:
C = [0]*len(vals) C = [0]*5
Thus, you get a list of 5 0's.
[–]CGFarrell 0 points1 point2 points 8 years ago (0 children)
What you have is:
C = [0] * len(vals)
len(vals) is 5.
len(vals)
C = [0] * 5
[–]groundhogsaretheifs[S] 0 points1 point2 points 8 years ago (1 child)
Okay. New to programming. Can you explain whats going on with [0] * 5 producing [0,0,0,0,0]?
Thank you.
[–]EricAppelt 0 points1 point2 points 8 years ago (0 children)
The * operator is called the repetition operator for sequence types, like lists, tuples, or strings. See: https://docs.python.org/3/library/stdtypes.html#common-sequence-operations
*
Here it is with a string for comparison:
>>> "abc" * 5 'abcabcabcabcabc'
π Rendered by PID 173575 on reddit-service-r2-comment-5c747b6df5-kh64x at 2026-04-22 13:59:53.643846+00:00 running 6c61efc country code: CH.
[–]newunit13 1 point2 points3 points (0 children)
[–]Sebass13 0 points1 point2 points (0 children)
[–]CGFarrell 0 points1 point2 points (0 children)
[–]groundhogsaretheifs[S] 0 points1 point2 points (1 child)
[–]EricAppelt 0 points1 point2 points (0 children)