Python Lambda

Python · Functions & OOP

Theory

A lambda is a small anonymous function written in a single expression: lambda arguments: expression. It has no name and no return keyword - the value of the expression is returned automatically. lambda x: x * x is the same as def square(x): return x * x, just shorter.

Lambdas shine when another function needs a short function as input. sorted(rows, key=lambda r: r[1]) sorts a list of (name, salary) tuples by salary; filter(lambda s: s > 100, sales) keeps only large sales; map(lambda s: s * 1.18, prices) adds 18% GST to every price. Because a lambda can hold only one expression, it cannot contain statements like if-blocks, loops or assignments (a conditional expression a if cond else b is allowed).

Why we use it

Many built-ins (sorted, min, max, map, filter) and pandas methods (apply) take a function as an argument. A lambda lets you write that throwaway function right where it is used instead of defining a named function elsewhere.

When to use it

Use a lambda for short, single-expression logic passed to another function, such as a sort key. If the logic needs several lines, a docstring or reuse in many places, write a normal def function instead.

How it works

  1. sorted(rows, key=lambda r: r[1])
  2. sorted() takes each tuple from rows
  3. Call the lambda on it to get the key r[1] (salary)
  4. Compare items by their keys
  5. Are all items placed in order? Yes: Return the new sorted list. No: Keep comparing keys.
  6. Result: rows ordered by salary

Syntax

lambda arguments: expression

Example

square = lambda x: x * x
square(5)  # 25

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic