About 50 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 - Sorting a set of values - Stack Overflow

    Since Python 3.7, dicts keep insertion order. So if you want to order a set but still want to be able to do membership checks in constant time, you can create a dictionary from the sorted list using …

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

    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 - 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 …

  5. 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 …

  6. 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 …

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

    Aug 3, 2010 · c2.sort(key = sort_key) If you want to sort on more entries, just make the key function return a tuple containing the values you wish to sort on in order of importance.

  8. 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 …

  9. python - How to sort a list/tuple of lists/tuples by the element at a ...

    @Cemre as for the second example, sort here is a method of List object of Python, which receives a lambda function as its key parameter. You may name it as tup, or t, or whatever you like and it'll still …

  10. 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.