About 503,000 results
Open links in new tab
  1. Sort a list in python - Stack Overflow

    Oct 5, 2013 · And the sort method has a parameter, called key, which you can pass a function. Using this parameter, your list won't be ordered by the values of the list, but by the values of your function on the list. In your case, you should use the abs () function, which will return the absolute value of your list elements. So, your list

  2. python - How do I sort a list of objects based on an attribute of …

    Feb 27, 2023 · Here I would use the variable name "keyfun" instead of "cmpfun" to avoid confusion. The sort () method does accept a comparison function through the cmp= argument as well.

  3. Sorting a list of lists in Python - Stack Overflow

    Aug 3, 2010 · How to sort a list/tuple of lists/tuples by the element at a given index (11 answers)

  4. python - How to sort a list of strings? - Stack Overflow

    Aug 30, 2008 · What is the best way of creating an alphabetically sorted list in Python?

  5. python - What is the difference between sorted (list) vs list.sort ...

    Use list.sort() when you want to mutate the list, sorted() when you want a new sorted object back. Use sorted() when you want to sort something that is an iterable, not a list yet. For lists, list.sort() is faster than sorted() because it doesn't have to create a …

  6. python - Sorting a list by conditional criteria - Stack Overflow

    Apr 28, 2017 · I know how to simply sort a list in Python using the sort() method and an appropriate lambda rule. However I don't know how to deal with the following situation : I have a list of strings, that e...

  7. Python list sort in descending order - Stack Overflow

    Mar 16, 2023 · This is a strange answer because you do the sorting in-place but then the reversing out-of-place. If there is another variable aliasing the original list, its value afterwards will not have the elements in their original order, nor in descending order; the alias will point at a list sorted in ascending order.

  8. python - Sort a part of a list in place - Stack Overflow

    Feb 16, 2010 · A good reason for in-place sort is a case where you want to sort the end of the list (that is already mostly sorted, perhaps by a less-expensive key function), and then pop the last value.

  9. python - How to sort a list of lists by a specific index of the inner ...

    I have a list of lists. For example, [ [0,1,'f'], [4,2,'t'], [9,4,'afsd'] ] If I wanted to sort the outer list by the string field of the inner lists, how would you do that in python?

  10. python - Why does "return list.sort ()" return None, not the list ...

    Python built-ins stick to one or the other; methods on immutable types like str do chain (they can't mutate in place, so they return a new object with the mutation applied), while most methods on mutable types like list don't. But for common cases like this, sorted exists to …

Refresh