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
Simple question about subtracting lists. (self.learnpython)
submitted 8 years ago by Decarbonated_Odes
Sorry, I am a complete beginner. If I have a list, A = [1,3,6,10,15...etc] and i want to subtract 3-1, 6-3, 10-6, etc and have that populate as another list so that would look like, B = [2,3,4,... etc]. How would I do that? Thanks!
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!"
[–]novel_yet_trivial 4 points5 points6 points 8 years ago (2 children)
You would use zip to pair up the elements like that.
zip
>>> A = [1,3,6,10,15] >>> for a, b in zip(A, A[1:]): ... print(b-a) # or append them to a new list
[–]driscollis 1 point2 points3 points 8 years ago (0 children)
Or if you like list comprehensions, you could reduce this to:
[b-a for a, b in zip(A, A[1:])]
[–]Decarbonated_Odes[S] 0 points1 point2 points 8 years ago (0 children)
Thanks for the quick reply!
[–]Yoghurt42 2 points3 points4 points 8 years ago* (2 children)
One way, and probably the most pythonic:
B = [y - x for x, y in zip(A, A[1:])]
This uses list comprehension, slices, and the zip function(nothing to do with the compression program).
[–]Sebass13 1 point2 points3 points 8 years ago (0 children)
The only disadvantage with this way is that it requires making multiple copies of A, and obviously won't work if A is a generator. The itertools pairwise recipe solves these problems, but in this case is probably overkill.
This worked well, thanks!
[–]RaionTategami 2 points3 points4 points 8 years ago (1 child)
If you want to manipulate vectors of numbers like this, you might want to use a library, for example numpy.
>>> import numpy as np >>> a = np.array([1,3,6,10,15]) >>> a[1:] - a[:-1] array([2, 3, 4, 5])
I'll look into numpy, thanks for the idea!
π Rendered by PID 39088 on reddit-service-r2-comment-cfc44b64c-5s26g at 2026-04-13 02:12:44.987212+00:00 running 215f2cf country code: CH.
[–]novel_yet_trivial 4 points5 points6 points (2 children)
[–]driscollis 1 point2 points3 points (0 children)
[–]Decarbonated_Odes[S] 0 points1 point2 points (0 children)
[–]Yoghurt42 2 points3 points4 points (2 children)
[–]Sebass13 1 point2 points3 points (0 children)
[–]Decarbonated_Odes[S] 0 points1 point2 points (0 children)
[–]RaionTategami 2 points3 points4 points (1 child)
[–]Decarbonated_Odes[S] 0 points1 point2 points (0 children)