How to use if/else in a dictionary comprehension?
In python2.7+ exist any way to make something like: { 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} Marcin 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