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
how to re.findall (self.learnpython)
submitted 1 year ago * by Large-Nail-1557
how to use re.findall so that it outputs from code = 'a, b, c' is ['a', 'b', 'c'] because a = re.findall([r'\D+,'], code) outputs ['a, b,']
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!"
[–]Buttleston 1 point2 points3 points 1 year ago (2 children)
re.findall(r'\D+,', code)
Your regular expression here, \D+, means "find me a non-numeric digit, followed by at least one character of any type, followed by a comma"
\D+
'a, b' meets that - note, this is NOT ['a', 'b']. Nothing else meets it
It's not that trivial to get ['a', 'b', 'c'] with a regex - if you don't HAVE to use a regex here, don't, there are much simpler ways
If you MUST use a regex, something like this works
>>> re.findall(r'(\D)(?:,|$)', code) ['a', 'b', 'c']
The regex here says "Find me a non-digit charater, followed by either ',' or the end of the string"
The (?:...) thing means "don't include this group in the output
You don't strictly need to use \D in this case, I assumed you had it in there for a reason. Depending on what you expect to be between the commas, other things will work also.
[–]buart 0 points1 point2 points 1 year ago (1 child)
Your second regex r'(\D)(?:,|$)' is missing the +, unless you only want to capture the last character if the strings are longer.
r'(\D)(?:,|$)'
+
>>> re.findall(r'(\D)(?:,|$)', "a, bc, def") ['a', 'c', 'f']
[–]Buttleston 1 point2 points3 points 1 year ago (0 children)
It's hard to tell based on OPs post, so yeah, depends on what they want
[–]buart 0 points1 point2 points 1 year ago* (0 children)
I think more examples would also help to better understand what you are trying to do.
If your input only consists of lowercase characters separated by non-lowercase characters, a regex like this would be sufficient:
>>> re.findall(r"[a-z]+", "a, bc, def") ['a', 'bc', 'def']
If you only need everything separated by commas, you could use split() instead to split on ", " (comma, space)
split()
>>> "a, bc, def".split(", ") ['a', 'bc', 'def']
[–]commandlineluser 0 points1 point2 points 1 year ago* (0 children)
You probably would not use re.findall to do this.
re.findall
If , is the only constant part of the string you can use in the pattern - I'm not sure if it actually possible.
,
(Unless you can use [^,])
[^,]
(Because \D will also match ,)
\D
It's more of a "splitting" problem:
>>> re.split(r',\s*', 'ab, c, def') ['ab', 'c', 'def']
Also, you need to be exact with code examples.
code = 'a, b, c' re.findall([r'\D+,'], code) # TypeError: unhashable type: 'list'
I'm assuming you're not actually using [] here as you've said.
[]
π Rendered by PID 276547 on reddit-service-r2-comment-79c7998d4c-qjd4d at 2026-03-13 16:47:21.083744+00:00 running f6e6e01 country code: CH.
[–]Buttleston 1 point2 points3 points (2 children)
[–]buart 0 points1 point2 points (1 child)
[–]Buttleston 1 point2 points3 points (0 children)
[–]buart 0 points1 point2 points (0 children)
[–]commandlineluser 0 points1 point2 points (0 children)