a = {"hello" : "world", "cat":"bat"}
# Trying to achieve this
# Form a new dictionary only with keys with "hello" and their values
b = {"hello" : "world"}
# This didn't work
b = dict( (key, value) if key == "hello" for (key, value) in a.items())
Any suggestions on how to include a conditional expression in dictionary comprehension to decide if key, value tuple should be included in the new dictionary
key == "..."or are you looking for a more generic solution? - icktoofayif key == "hello"is not a conditional expression anddict(...)not a dict comprehension. - Ashwini Chaudhary//is not a valid python comment - noɥʇʎԀʎzɐɹƆ