How can I use if/else in a dictionary comprehension?
Solution 1
You've already got it: A if test else B
is a valid Python expression. The only problem with your dict comprehension as shown is that the place for an expression in a dict comprehension must have two expressions, separated by a colon:
{ (some_key if condition else default_key):(something_if_true if condition
else something_if_false) for key, value in dict_.items() }
The final if
clause acts as a filter, which is different from having the conditional expression.
Solution 2
@Marcin's answer covers it all, but just in case someone wants to see an actual example, I add two below:
Let's say you have the following dictionary of sets
d = {'key1': {'a', 'b', 'c'}, 'key2': {'foo', 'bar'}, 'key3': {'so', 'sad'}}
and you want to create a new dictionary whose keys indicate whether the string 'a'
is contained in the values or not, you can use
dout = {"a_in_values_of_{}".format(k) if 'a' in v else "a_not_in_values_of_{}".format(k): v for k, v in d.items()}
which yields
{'a_in_values_of_key1': {'a', 'b', 'c'},
'a_not_in_values_of_key2': {'bar', 'foo'},
'a_not_in_values_of_key3': {'sad', 'so'}}
Now let's suppose you have two dictionaries like this
d1 = {'bad_key1': {'a', 'b', 'c'}, 'bad_key2': {'foo', 'bar'}, 'bad_key3': {'so', 'sad'}}
d2 = {'good_key1': {'foo', 'bar', 'xyz'}, 'good_key2': {'a', 'b', 'c'}}
and you want to replace the keys in d1
by the keys of d2
if there respective values are identical, you could do
# here we assume that the values in d2 are unique
# Python 2
dout2 = {d2.keys()[d2.values().index(v1)] if v1 in d2.values() else k1: v1 for k1, v1 in d1.items()}
# Python 3
dout2 = {list(d2.keys())[list(d2.values()).index(v1)] if v1 in d2.values() else k1: v1 for k1, v1 in d1.items()}
which gives
{'bad_key2': {'bar', 'foo'},
'bad_key3': {'sad', 'so'},
'good_key2': {'a', 'b', 'c'}}
Solution 3
In case you have different conditions to evaluate for keys and values, @Marcin's answer is the way to go.
If you have the same condition for keys and values, you're better off with building (key, value)-tuples in a generator-expression feeding into dict()
:
dict((modify_k(k), modify_v(v)) if condition else (k, v) for k, v in dct.items())
It's easier to read and the condition is only evaluated once per key, value.
Example with borrowing @Cleb's dictionary of sets:
d = {'key1': {'a', 'b', 'c'}, 'key2': {'foo', 'bar'}, 'key3': {'so', 'sad'}}
Assume you want to suffix only keys
with a
in its value
and you want the value
replaced with the length of the set in such a case. Otherwise, the key-value pair should stay unchanged.
dict((f"{k}_a", len(v)) if "a" in v else (k, v) for k, v in d.items())
# {'key1_a': 3, 'key2': {'bar', 'foo'}, 'key3': {'sad', 'so'}}
Solution 4
It also might be worth mentioning that If only statements put the if at the end:
{_ for _ in iterable if True}
Related videos on Youtube
Comments
-
diegueus9 almost 3 years
Does there exist a way in Python 2.7+ to make something like the following?
{ something_if_true if condition else something_if_false for key, value in dict_.items() }
I know you can make anything with just 'if':
{ something_if_true for key, value in dict_.items() if condition}
-
mdeous about 12 yearsas told by @Marcin, a
dict
is made ofkey:value
elements, you're not building adict
here but aset
(see set literals).
-
-
Jeremy Weirich almost 8 yearsWorth mentioning that you don't need to have an if-else condition for both the key and the value. For example,
{(a if condition else b): value for key, value in dict.items()}
will work. -
Marcin almost 8 years@JeremyWeirich You don't need to have an if-else for either of them if you don't want to.
-
alancalvitti about 5 yearsfor your second example using
d1
,d2
I getAttributeError: 'dict_values' object has no attribute 'index'
-
Cleb about 5 years@alancalvitti: thanks for pointing this out! The solution was for Python 2 and does not work for Python 3; I added a Python 3 solution too.
-
Stan11 almost 4 years@Marcin Is it possible for me to use only "if" for the key part and use both "if" and "else" for the value part?
-
Jeyekomon almost 3 yearsThe parentheses around the expression are not necessary (though recommended indeed).
-
physincubus over 2 yearsNice, this is exactly what I was looking for. Its a shame there isn't a pythonic way to keep the syntax identical and then just do something like
else pass