0

I am trying to decode/understand a python script, I have this line:

sorted_list = [ i for i in sorted(my_list,key=lambda x: x.ram_address if x.ram_address != None else None) if i.ram_address != None]

with next error message:

'<' not supported between instances of 'NoneType' and 'int'

I kind understand that because the function "sorted" is trying to do some comparison '<' in order to sort the list, and I might have some 'none' values

Can you help me to understand the entire line what it does, and how can I solve the problem with None type and got the same result??, this problem rised in python 3.6, but there is no problem with python 2.6

Nick
  • 138,499
  • 22
  • 57
  • 95
  • Does this answer your question? [Python None comparison: should I use "is" or ==?](https://stackoverflow.com/questions/14247373/python-none-comparison-should-i-use-is-or) – Nick Jul 03 '20 at 02:18
  • Use `is not None`, not `!= None` – Nick Jul 03 '20 at 02:18
  • But you also need a default that is not `None` in this piece of code: `x.ram_address if x.ram_address != None else None` otherwise you will continue to get the same error. – Nick Jul 03 '20 at 02:22
  • The `if i.ram_address != None` at the end suggests you want to filter those values out of the results, but you need to apply the filter *before* you sort, or it won't prevent the sort from having issues. Try filtering first, then sorting with `key=lambda x: x.ram_address`. Actually, your existing key is identical to this, it just does pointless check for `None` before passing it on anyway. – Blckknght Jul 03 '20 at 03:01

0 Answers0