Search code examples
pythonpython-2.7sortingnonetype

Sort list with None in Python getting the index


How to sort a list with None value in Python getting the index?

myList=['BAT46ZFILM', 'CMD17-21VGD/TR8', 'BAT46ZFILM', None, 'B72207S2321K311']

I know that

[i[0] for i in sorted(enumerate(myList), key=lambda x:x[1])]

get the index of the sort method. But how put all the None value at the end. For example, merging with this solution:

sorted(myList, key=lambda x: (x is None, x))

that sort with None value but do not get me the indexes.


Solution

  • Using

    sorted(range(len(m)), key=lambda k:(m[k] is None,  m[k]))