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.
In more detail
Lambdas are handy where a short function is needed once, such as a sort key. Anything longer or reused deserves a named function defined with def.
Examples
lambda v: json.dumps(v).encode()sorted(pairs, key=lambda p: p[1])Related terms
- serialization — Turning an in-memory object into bytes, such as JSON text, that can be sent or stored, and parsing it back again.
- key function — A function given as key= to sorted, sort, min or max, which turns each item into the value to compare by.
- function — A named, reusable block of code that takes inputs, called arguments, and can return a result.
- def — The Python keyword that starts a function definition: def, the name, parameters in parentheses, a colon, then an indented body.
Taught in
- Ninety Minutes, No Question Count — Confluent Apache Kafka Certifications (CCDAK and CCAAK) Exam Prep
- The Alert That Came An Hour Late — Confluent Apache Kafka Certifications (CCDAK and CCAAK) Exam Prep
- The New Broker That Did Nothing — Confluent Apache Kafka Certifications (CCDAK and CCAAK) Exam Prep
- Forty One Under-Replicated Partitions — Confluent Apache Kafka Certifications (CCDAK and CCAAK) Exam Prep
- Seventy One Percent — Confluent Apache Kafka Certifications (CCDAK and CCAAK) Exam Prep
- Keys And Ordering Per Partition — Apache Kafka & Event Streaming
- Duplicates Are The Default — Apache Kafka & Event Streaming
- Literals, Identifiers And Keywords — 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 Class That Shared One List — 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
- Comparing, Slicing And Never Changing — 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
- Closures: Functions That Remember — 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