sorted()
sorted(items) returns a new sorted list from any iterable; items.sort() sorts a list in place and returns None.
In more detail
Both accept key= for a custom ordering and reverse=True for descending order. Writing items = items.sort() loses your list, because sort returns None.
Example
sorted(["pear", "Fig", "apple"], key=str.lower)Related terms
- key function — A function given as key= to sorted, sort, min or max, which turns each item into the value to compare by.
- stable sort — A sort that keeps items with equal keys in the same relative order they had before sorting.
- lambda — A small function written inline as an expression, such as lambda x: x * 2, with no name and a single expression as its body.
Taught in
- Appending To Lists, Sets, Constructors — Hands-on Python: Complete Video Course & Book
- Lists, Tuples, Sets, Dicts: Choosing One — Hands-on Python: Complete Video Course & Book
- Comprehensions — Hands-on Python: Complete Video Course & Book
- Sorting, Keys And Lambdas — Hands-on Python: Complete Video Course & Book
- The Exception Family Tree — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Nested Loops, Traced By Hand — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Lists: Slices, Methods And Aliases — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Dictionaries: Keys, Values And Items — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Collections Sprint — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Mock, Review And Exam Day — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Import Variants, dir() And sys.path — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- math, random And platform — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Modules Sprint — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Comparing, Slicing And Never Changing — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- String Methods The Exam Picks — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Strings Sprint — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Class Variables, Instance Variables And __dict__ — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Special Methods: __str__, __eq__ And Friends — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Object-Oriented Sprint — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Lambdas, map() And filter() — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Comprehensions, Closures And Files Sprint — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Twelve Traps, Mostly Objects — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Mock, Review And Exam Day — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep