0

I have a list, which is appended with integer and float values. I want to sort that list based on the integer value to get min and max values of the floats:

my_list = [['first', -20, 'second', 4.6429111469868918],
['first', -19, 'second', 4.6478324005489107],
['first', -18, 'second', 4.652294108548074],
['first', -17, 'second', 4.6562933144428431]]

min_int = my_list.sort(key=lambda x:x[1],reverse=True)
max_int = my_list.sort(key=lambda x:x[1],reverse=False)

print("min_int: ", min_int)
print("max_int: ", max_int)

The sort with the key results:

min_int: None 
max_int: None

I don't understand the reason of that. Could someone can help?

mystic.06
  • 189
  • 8
  • https://stackoverflow.com/questions/22442378/what-is-the-difference-between-sortedlist-vs-list-sort – PM 77-1 Dec 23 '21 at 15:35
  • 1
    It is because `my_list.sort` doesn't return sorted value but rather sort the list in place. You can get the min, max value by accessing the first and last value, respectively. You can try something like `my_list.sort(key=lambda x:x[1]); min_int=my_list[0]; max_int=my_list[-1]` – tax evader Dec 23 '21 at 15:38
  • 1
    You're sorting my_list in place by using .sort(), but it doesn't return anything. So to get the min and max, you could do .sort() and take the first and last entries of my_list. – Benjamin Rowell Dec 23 '21 at 15:39

0 Answers0