min() and max()
Built-in functions that return the smallest or largest item of an iterable, or of several arguments; sum adds them up.
In more detail
min and max accept key= like sorted, so max(words, key=len) finds the longest word. sum has no key; use a generator expression instead, as in sum(len(w) for w in words).
Example
max(["fig", "banana"], key=len)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.
- sorted() — sorted(items) returns a new sorted list from any iterable; items.sort() sorts a list in place and returns None.
Taught in
- Types And Functions, A Whirlwind Tour — Hands-on Python: Complete Video Course & Book
- Packages, And Your Own Library — Hands-on Python: Complete Video Course & Book
- Sorting, Keys And Lambdas — Hands-on Python: Complete Video Course & Book
- Iterators And Generators — Hands-on Python: Complete Video Course & Book
- Forty Minutes, Thirty Items: How PCEP Is Run — 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
- Sixty Five Minutes, Forty Items: How PCAP Is Run — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Strings Sprint — 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
- Lambdas, map() And filter() — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Generators And Iterators — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep