About 166,000 results
Open links in new tab
  1. How does the key argument in python's sorted function work?

    The function you pass in to key is given each of the items that are being sorted, and returns a "key" that Python can sort by. So, if you want to sort a list of strings by the reverse of the string, you could do this: list_of_strings.sort(key=lambda s: s[::-1]) This lets you specify the value each item is sorted by, without having to change the ...

  2. python - How do I sort a dictionary by key? - Stack Overflow

    Jan 26, 2017 · Note: for Python 3.7+, see this answer Standard Python dictionaries are unordered (until Python 3.7). Even if you sorted the (key,value) pairs, you wouldn't be able to store them in a dict in a way that would preserve the ordering.

  3. python - Syntax behind sorted(key=lambda: ...) - Stack Overflow

    Key Functions Starting with Python 2.4, both list.sort() and sorted() added a key parameter to specify a function to be called on each list element prior to making comparisons. We have to do a little bit of reading between the lines here, but what this tells us is that the sort function is only called once, and if we specify the key argument ...

  4. sorting python list based on key - Stack Overflow

    Jan 7, 2013 · key specifies a function of one argument that is used to extract a comparison key from each list element. eg. def combined_date_key(elem): return datetime.datetime.combine(v.created_date, v.created_time) entries.sort(key=combined_date_key) (this is identical to the lambda already posted, just broken out for clarity).

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

    Feb 27, 2023 · Readers should notice that the key= method: ut.sort(key=lambda x: x.count, reverse=True) is many times faster than adding rich comparison operators to the objects. I was surprised to read this (page 485 of "Python in a Nutshell"). You can confirm this by running tests on this little program:

  6. JSON output sorting in Python - Stack Overflow

    If you specify sort_keys=False then Python will simply print the items in whatever order they appear in the underlying Python dict object. In some cases this may happen to be the same as the default alphanumeric sort order. In your example, the keys AREN'T even sorted like that, since "format" comes after "request".

  7. How do I perform secondary sorting in python? - Stack Overflow

    Aug 19, 2016 · If i have a list of numbers [4,2,5,1,3] I want to sort it first by some function f and then for numbers with the same value of f i want it to be sorted by the magnitude of the number. This code do...

  8. Sorting a Python list by two fields - Stack Overflow

    employees.sort(key = lambda x:x[1]) employees.sort(key = lambda x:x[0]) We can also use .sort with lambda 2 times because python sort is in place and stable. This will first sort the list according to the second element, x[1]. Then, it will sort the first element, x[0] (highest priority).

  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 cmp function returns a non-zero return if so break and send the return value. You call it by calling a Lambda of a function of a list of Lambdas.

  10. How to 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 than 12 years old. –