I'd like to use dict comprehensions and the ternary operator to create a new dict. Here is a MWE.
# Step 1: Create a dict, `d`
# {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f', 7: 'g', 8: 'h', 9: 'i', 10: 'j'}
import string
d = {k : v for k, v in zip(range(1, 11), string.ascii_lowercase[:10])}
# Step 2: Use dict comprehensions and the ternary operator to get a new dict
new_dict = {k : v if k<5 else v : k for k, v in d.items()}
but encounter this issue,
new_dict = {k : v if k<5 else v : k for k, v in d.items()}
^
SyntaxError: invalid syntax
Why is SyntaxError
rasied? How to fix it?