4

I've dictionary containing list of key:value pairs. How to sort the list containing key:value pairs (where value is None)

from operator import itemgetter
test = {"test":[{ "name" : "Nandini", "age" : 20}, { "name" : "Manjeet", "age" : 20 }, { "name" : None , "age" : 19 }] }
print(sorted(test["test"], key=itemgetter('name')) )

Results to TypeError: '<' not supported between instances of 'NoneType' and 'str'

Output should include None values and look like: [{ "name" : None , "age" : 19 }, { "name" : "Manjeet", "age" : 20 },{ "name" : "Nandini","age" : 20}]

1 Answer 1

11

You can use the "or" trick:

# python 2 you can use with the itemgetter('name') 
# python3 needs a lambda, see further below
from operator import itemgetter
test = {"test":[{ "name" : "Nandini", "age" : 20}, 
                { "name" : "Manjeet", "age" : 20 }, 
                { "name" : None , "age" : 19 }] }


# supply a default value that can be compared - here "" is a good one
print(sorted(test["test"], key=itemgetter('name') or "") )

to get:

[{'age': 19, 'name': None}, 
 {'age': 20, 'name': 'Manjeet'}, 
 {'age': 20, 'name': 'Nandini'}]

You essentially supply a default value for None`s (and empty strings - it operates on the Truthy-ness of the value).

For python 3 you would need to use a lambda instead:

print(sorted(test["test"], key=lambda x: x['name'] or ""  ))  

More infos:

Here the lambda is kindof like the itemgetter() - x is each inner dict of the list, x["name"] is the value you want as key and if that is Falsy (None, "") it will use whatever you supply after the or:

print(None or "works")
print("" or "works as well")
2
  • 2
    @Andrej .. should have switched to python 3 instead of python 2 - my default tester "pyfiddle.io" defaults to python 2 :/ - yep in python 3 you would need the lambda. Edited. Commented Jun 9, 2020 at 19:48
  • 1
    Thanks @PatrickArtner. For Python3, lambda worked. Could you explain that lambda part ?
    – StackGuru
    Commented Jun 9, 2020 at 19:53

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.