given a dict of items
sample = { 'a':1 , 'b':2 , 'c':3 , 'd':None , 'e':None }
i’d like to create a new dict that only has the items with a value.
this works..
a = dict([ i for i in testing.iteritems() if i[1]])
but i’m wondering if there’s something more pythonic/readable.
i was hoping i could do something like
a = dict([ i for i in testing.____() if i.value() ])
but there’s no standard iterator that works like that.
i’m just curious on this for readability purposes.
Use tuple unpacking (assigning each key and value to a separate variable,
kandvin the following examples) and a dict comprehension:or test explicitly for
Noneinstead of all falsey values (numeric 0, empty sequences, etc.):In Python 3, you need to use just
items():For your sample, all above variations result in: