For python2 and python3 both, you can do as follows, where I assume if the value of p
is missing, I am assuming sys.maxsize
value, which will ensure that if we have missing values for p
, they are always at the end of the list
import sys
a = [
{'p': 3, 'id': 'ab1'},
{'p': 10, 'id': 'ab2'},
{'p': 5, 'id': 'ab3'},
{'p': 8, 'id': 'ab4'},
{'id': 'ab5'}
]
print(sorted(a, key=lambda i: i.get('p', sys.maxsize)))
Which will give you
[{'p': 3, 'id': 'ab1'}, {'p': 5, 'id': 'ab3'}, {'p': 8, 'id': 'ab4'}, {'p': 10, 'id': 'ab2'}, {'id': 'ab5'}]
Another solution if comparing to sys.maxsize
is overkill, is to separate out the dictionaries without the key p
, sort the remaining dictionary which contain the key p
, and then extend it by attaching the non p
key dictionaries like so. This also avoids us from providing a default value for p
in item.get()
import sys
a = [
{'p': 3, 'id': 'ab1'},
{'p': 10, 'id': 'ab2'},
{'p': 5, 'id': 'ab3'},
{'p': 8, 'id': 'ab4'},
{'id': 'ab5'}
]
#Dictionaries with key p
p_items = [item for item in a if 'p' in item.keys()]
#Dictionaries not with key p
non_p_items = [item for item in a if 'p' not in item.keys()]
#Sort the p-key dictionaries
result = sorted(p_items, key=lambda i: i.get('p'))
#Attach non p-key dictionay items at end
result.extend(non_p_items)
print(result)