I want to be able to add an attribute to a dictionary but only if the condition I pass in is true. For example:
def addSum(num):
obj = {
'name': "Home",
'url': "/",
num > 0 ? 'data': num
}
Is this possible? I can't find a way to do this in python, I have only seen examples in javascript.
obj = {
'name': 'Home',
'url': '/',
**({'data': num} if num > 0 else {})
}
:D
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With