About 51 results
Open links in new tab
  1. 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 …

  2. python - Sort a list of numbers by absolute value (ignoring ...

    l.sort(key= abs, reverse = True) Lists can be sorted using the sort () method. And the sort method has a parameter, called key, which you can pass a function. Using this parameter, your list won't be …

  3. python - How do I sort a list of objects based on an attribute of the ...

    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.

  4. Python list sort in descending order - Stack Overflow

    Apr 20, 2010 · 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 …

  5. Python sort () method on list vs builtin sorted () function

    The above snippet showed something interesting that, python's sort function behaves very well for already sorted data. As pointed out by Anurag, in the first case, the sort method is working on …

  6. How can I sort a list of dictionaries by a value of the dictionary in ...

    Python has supported the key= for .sort since 2.4, that is year 2004, it does the Schwartzian transform within the sorting code, in C; thus this method is useful only on Pythons 2.0-2.3. all of which are more …

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

    Aug 30, 2008 · As sort() sorts the list in place (ie, changes the list directly), it doesn't return the sorted list, and actually doesn't return anything, so your print statement prints None. If you saved your list to …

  8. python - Sort multidimensional array based on 2nd element of the ...

    Sort multidimensional array based on 2nd element of the subarray Asked 12 years, 3 months ago Modified 1 year, 2 months ago Viewed 132k times

  9. python - Sort a list by multiple attributes? - Stack Overflow

    Nov 20, 2010 · Here's one way: You basically re-write your sort function to take a list of sort functions, each sort function compares the attributes you want to test, on each sort test, you look and see if the …

  10. python - What is the complexity of the sorted () function? - Stack …

    Jan 21, 2013 · I have a list of lists and I am sorting them using the following data=sorted(data, key=itemgetter(0)) Was wondering what is the runtime complexity of this python function?