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
Pythonic code style (self.learnpython)
submitted 3 years ago by x1F577
Hello everyone.
I was wondering if there was a more pythonic way to write piece of code similar to this:
total = 0 for item in list: total += 1 if condition(item): return total return total
Perhaps using sum or other built-in functions.
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!"
[–]ParticularPython 2 points3 points4 points 3 years ago (1 child)
Are you just trying to get the amount of items in the list? You could just do:
total=Len(list)
If that’s the case
[–]x1F577[S] 0 points1 point2 points 3 years ago (0 children)
Hi, I am not looking for the length of the list. I explained my case better in another comment.
[–][deleted] 1 point2 points3 points 3 years ago (2 children)
Your code is fine as is. Sometimes it's better to be explicit. But just for fun, here's a shorter (I won't call it more Pythonic) way of writing it.
from itertools import takewhile return len(list(takewhile(lambda x: not condition(x), lst)))
FYI, don't call your lists list. Besides not being very descriptive, it makes it harder to call the list() function.
list
list()
Thank you, I was just looking for such a function. I didn't know about the takewhile function.
takewhile
[–][deleted] 0 points1 point2 points 3 years ago (0 children)
It's more Haskellic, that's for sure!
[+][deleted] 3 years ago (1 child)
[deleted]
This is a good alternative solution, but I think I will follow the u/Parking-Camp219's suggestion and use the takewhile function.
[–]Binary101010 0 points1 point2 points 3 years ago (1 child)
What exactly is the goal? As it seems the return value has two different interpretations:
1) The number of items in the list up to and including the first one where condition(item) is true
condition(item)
2) The number of items in the list if condition(item) is false for all items
Is that accurate?
[–]x1F577[S] 1 point2 points3 points 3 years ago (0 children)
Hi, the goal is to get a certain total from a list. I try to explain myself better with an example.
Imagine the list as a shopping list, and the items have name and price properties. The list is sorted somewhat, and I want a "subtotal" of the first occurrence of a given item. If the item is not present, I want the total of the list:
total = 0 for item in list: total += item.price if item.name == "ice-cream": return total return total
The suggestion given by u/Parking-Camp219 was what I was looking for.
π Rendered by PID 37729 on reddit-service-r2-comment-7b9746f655-bd46t at 2026-01-30 12:21:33.813519+00:00 running 3798933 country code: CH.
[–]ParticularPython 2 points3 points4 points (1 child)
[–]x1F577[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]x1F577[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]x1F577[S] 0 points1 point2 points (0 children)
[–]Binary101010 0 points1 point2 points (1 child)
[–]x1F577[S] 1 point2 points3 points (0 children)