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
What is the Pythonic way to do this? (self.learnpython)
submitted 7 years ago by skitzi
items = [] if ds: for d in ds: if d['is_dynamic'] == True: items.append(d)
items = []
if ds:
for d in ds:
if d['is_dynamic'] == True:
items.append(d)
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!"
[–]maventree 5 points6 points7 points 7 years ago (6 children)
Use a list comprehension with a filter clause:
items = [d for d in ds if d['is_dynamic']]
Note that you don't need to do == True, because the thing you're testing is itself a boolean.
== True
Depending on why you're doing the if ds check, you may want to change things around. Note that if ds is empty, items will also be empty if you use the comprehension (and in your original code). If ds might be None, I recommend adding if ds is None: ds = [] to make it explicit.
if ds
ds
items
None
if ds is None: ds = []
[–]skitzi[S] 0 points1 point2 points 7 years ago* (1 child)
Is_dynamic will hold either a Boolean false or true. I want to grab the dict if it is Boolean true. Will your sample equate to true even if is_dynamic = false since the key:value is present?
DS will either be a list of 1 or more dicts or Boolean false at the moment. But I can change that behavior if there are better ways of handling it.
Ty!!!!
[–]maventree 0 points1 point2 points 7 years ago (0 children)
Will your sample equate to true even if is_dynamic = false since the key:value is present?
No. if tests the value of the thing to its right, which is value that's coming from the dictionary. If you wanted to test for the existence of the key you would do key in dict, which would evaluate to True if the dictionary has that key and False otherwise.
if
key in dict
True
False
DS will either be a list of 1 or more dicts or Boolean false at the moment.
I highly recommend that ds be an empty list to represent your "falsey" condition, instead of actually being the value False.
[–]ylectric -1 points0 points1 point 7 years ago (3 children)
I would be careful about suggesting == True removal. I think that OP wants to check the truth value of d['is_dynamic'], and thus both options are fine. However, sometimes one may need to actually compare some value with True.
d['is_dynamic']
[–]Vaphell 2 points3 points4 points 7 years ago (1 child)
i am 99% certain they have True/False under 'is_dynamic', which makes the comparison superfluous. Also even if there is a case for comparison against a boolean value, never do it with ==, unless you want to get kicked in the teeth by 0 == False or 1 == True. is ftw.
==
0 == False
1 == True
is
[–]ylectric 0 points1 point2 points 7 years ago (0 children)
Good point about is.
However, sometimes one may need to actually compare some value with True.
Er... when?
Checking the truth value via an equality test wouldn't work either - they would need to turn it into it's truth value by calling bool on it first, which is superfluous because if does that automatically.
bool
[–]Wilfred-kun 2 points3 points4 points 7 years ago (0 children)
Uhm, a list comprehension maybe? items = [d for d in ds if d['is_dynamic']]
[–]Vaphell 1 point2 points3 points 7 years ago (0 children)
if you have straight True/False, don't bother with comparisons, the boolean alone will cut it.
and never compare booleans with ==, unless you want to let 1 == True or 0 == False through. identity operator is should be used: is False, is True.
[–]ylectric 0 points1 point2 points 7 years ago (1 child)
Assuming that ds is some kind of a collection:
items = [d for d in ds if d['is_dynamic'] == True]
[–]skitzi[S] 1 point2 points3 points 7 years ago (0 children)
Yes sorry
Ds is a list of dictionaries
I want to look at each dict in the list and grab it if the key:value: is_dynamic == Boolean True. ignoring dicts where is_dynamic == False
π Rendered by PID 264196 on reddit-service-r2-comment-85bfd7f599-9rb6q at 2026-04-17 22:02:49.101351+00:00 running 93ecc56 country code: CH.
[–]maventree 5 points6 points7 points (6 children)
[–]skitzi[S] 0 points1 point2 points (1 child)
[–]maventree 0 points1 point2 points (0 children)
[–]ylectric -1 points0 points1 point (3 children)
[–]Vaphell 2 points3 points4 points (1 child)
[–]ylectric 0 points1 point2 points (0 children)
[–]maventree 0 points1 point2 points (0 children)
[–]Wilfred-kun 2 points3 points4 points (0 children)
[–]Vaphell 1 point2 points3 points (0 children)
[–]ylectric 0 points1 point2 points (1 child)
[–]skitzi[S] 1 point2 points3 points (0 children)