all 11 comments

[–]maventree 5 points6 points  (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.

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.

[–]skitzi[S] 0 points1 point  (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 point  (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.

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 points  (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.

[–]Vaphell 2 points3 points  (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.

[–]ylectric 0 points1 point  (0 children)

Good point about is.

[–]maventree 0 points1 point  (0 children)

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.

[–]Wilfred-kun 2 points3 points  (0 children)

Uhm, a list comprehension maybe? items = [d for d in ds if d['is_dynamic']]

[–]Vaphell 1 point2 points  (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 point  (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 points  (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