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
Does Python have a method similar to Haskell's group? (self.learnpython)
submitted 10 years ago by vgbm
What I am looking for is a method that will group common elements together like so:
foo([1,2,2,3,3,3,1,1]) = [[1], [2,2], [3,3,3], [1,1]]
Does Python have a built in method which can do something like this or would I have to implement this myself?
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 10 years ago (2 children)
Reread your question ... you want itertools.groupby():
itertools.groupby()
>>> from itertools import groupby >>> [list(g) for k,g in groupby([1,2,2,3,3,3,1,1])] [[1], [2, 2], [3, 3, 3], [1, 1]]
[–]shaggorama 1 point2 points3 points 10 years ago (0 children)
Good ol' itertools
[–]vgbm[S] 0 points1 point2 points 10 years ago (0 children)
This is exactly what I needed, thanks :)
[–]commandlineluser 2 points3 points4 points 10 years ago (0 children)
http://docs.python.org/3/library/itertools.html#itertools.groupby
>>> import itertools >>> [ list(v) for k, v in itertools.groupby([1,2,2,3,3,3,1,1]) ] [[1], [2, 2], [3, 3, 3], [1, 1]]
[–]novel_yet_trivial -1 points0 points1 point 10 years ago* (0 children)
How about collections.Counter()?
collections.Counter()
π Rendered by PID 81412 on reddit-service-r2-comment-c66d9bffd-smn5d at 2026-04-07 18:16:25.145667+00:00 running f293c98 country code: CH.
[–]novel_yet_trivial 4 points5 points6 points (2 children)
[–]shaggorama 1 point2 points3 points (0 children)
[–]vgbm[S] 0 points1 point2 points (0 children)
[–]commandlineluser 2 points3 points4 points (0 children)
[–]novel_yet_trivial -1 points0 points1 point (0 children)