python - Python3: Conditional extraction of keys from a dictionary with comprehension -


i need extract keys of dictionary values pass condition. basically, want this, in shorter, more pythony way:

keys=[] key in dict:     if dict[key]==true:         keys.append(key) 

this original idea, raises valueerror:

[key (key,val) in map if val==true] 

i came moment, can't feeling it's not nice:

[key key in map.keys() if map[key]==true] 

is there less messy way it? perhaps obvious i'm missing?

thanks!

here way keys true values lot shorter , cleaner comprehension (not comprehensions bad though):

>>> dct = {0:false, 1:true, 2:false, 3:true} >>> list(filter(dct.get, dct)) [1, 3] >>> 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -