3

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?

6
  • 2
    What's the obsession with a one-liner? Your working solution works. Use it. Easy to read and functional trumps one-liner. Commented May 14, 2014 at 19:44
  • 1
    @g.d.d.c -- Especially when the one liner wouldend up being really obtuse. However, I'm pretty sure that OP's code doesn't work (the function doesn't return anything, references all sorts of globals, etc.) Commented May 14, 2014 at 19:46
  • okay, you tell me the best way to achieve this result. Commented May 14, 2014 at 19:46
  • You want to convert 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. Commented May 14, 2014 at 21:19
  • 1
    @msv How can you know, that converting None to "" gains you nothing. Working with "" and None may be in many situation significant difference and OP has right to ask for such a change. Commented May 14, 2014 at 21:35

2 Answers 2

4
lst=[{'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",}]

print [{key: val if val else "" for key, val in dct.items()} for dct in lst]

explained:

dct = lst[0]
{'applicationName': "ATM Monitoring",
                     'roamingDrop': "",
                     'noOfCustomer': None,
                     'ipAddress': "192.168.1.1",
                      'url': "www.google.co.in",}

Using dictionary comprehension (available since Python 2.7), first just reconstructing the dictionary into the same value:

{key: val  for dct.items()}

and extending it by assigning "" in case, we have as original value None (or any other value evaluating to False)

{key: val if val else ""  for dct.items()}

Finally (as shown above) it is applied in enveloping list comprehension to all items in the list.

{key: val  for dct.items()}

Strictly speaking, this replaces anything, what looks as boolean False by "".

If we want only None values replaced by "", and e.g. False and 0 keep as it is, we shall e more strict:

print [{key: val if val is not None else "" for key, val in dct.items()} for dct in lst]
Sign up to request clarification or add additional context in comments.

Comments

-1

This seems easy enough:

>>> for d in object_list:
...     for k, v in d.items():
...         if v is None:
...             d[k] = ''
... 

and to show what the output looks like:

>>> import pprint
>>> pprint.pprint(object_list)
[{'applicationName': 'ATM Monitoring',
  'ipAddress': '192.168.1.1',
  'noOfCustomer': '',
  'roamingDrop': '',
  'url': 'www.google.co.in'},
 {'applicationName': '',
  'ipAddress': '192.168.1.1',
  'noOfCustomer': '',
  'roamingDrop': '',
  'url': 'www.google.co.in'}]

3 Comments

This almost what i know .
actually you can safely use d.items() in python3, as they are views: docs.python.org/3/library/stdtypes.html#dictionary-view-objects
@m.wasowski -- You appear to be correct. At first, I thought you wouldn't be able to because they are views, but apparently I was incorrect ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.