How to get the desired result in python one liner ??
object_list=[{'applicationName': "ATM Monitoring",
'roamingDrop': "",
'noOfCustomer': None,
'ipAddress': "192.168.1.1",
'url': "www.google.co.in",},
{'applicationName': None,
'roamingDrop': "",
'noOfCustomer': None,
'ipAddress': "192.168.1.1",
'url': "www.google.co.in",}]
Result required is to replace all None to ""
object_list=[{'applicationName': "ATM Monitoring",
'roamingDrop': "",
'noOfCustomer': "",
'ipAddress': "192.168.1.1",
'url': "www.google.co.in",},
{'applicationName': "",
'roamingDrop': "",
'noOfCustomer': "",
'ipAddress': "192.168.1.1",
'url': "www.google.co.in",}]
Simple Function to make this happens is :
def simple():
for object in object_list:
for key, value in object.iteritems():
if value:
dict( object, **{key: value})
else:
dict(object, **{key: ''})
And Python one unsuccessful one liner:
[dict(object, **{key: value}) if value else dict(object, **{key: ''})
for object in object_list for key, value in object.iteritems()]
Can the one liner be achieved with list comprehensions?
Nones to''. This loses information and gains you nothing. It's unclear why you want to do this and even less clear why you want to do it in one line.