dict.pop()

Python · Dictionaries

Theory

.pop(key) removes key from a dictionary and returns its value, so you can delete and use the value in one step. With employee = {'name': 'Asha', 'salary': 65000}, salary = employee.pop('salary') gives salary = 65000 and leaves employee as {'name': 'Asha'}. If the key is missing it raises KeyError, unless you pass a default: employee.pop('bonus', 0) returns 0 and changes nothing.

Removal by key is O(1) on average, thanks to hashing. Compare it with del employee['salary'], which also removes the key but does not give the value back, and with popitem(), which removes and returns the most recently inserted (key, value) pair and is useful for processing a dict until it is empty. Never pop keys from a dict while looping over it directly; Python raises RuntimeError because the size changed. Loop over list(d) instead, or build a new dict with a comprehension.

Why we use it

Moving a field from one place to another, extracting a value before saving the rest, or consuming items from a work dict all need 'remove and give me the value'. pop() does exactly that, safely with a default.

When to use it

Use pop(key) when you need the removed value, pop(key, default) when the key may not exist, and del d[key] when you do not need the value. Use popitem() to remove the last inserted pair.

How it works

  1. Call employee.pop('salary')
  2. Hash 'salary' and look it up
  3. Is the key present? Yes: Remove the pair and return its value. No: Go to the default check.
  4. Was a default given? Yes: Return the default, dict unchanged. No: Raise KeyError.
  5. print(salary, employee) shows 65000 {'name': 'Asha'}

Syntax

dict.pop(key, default)

Example

employee = {'name': 'Asha', 'salary': 65000}
salary = employee.pop('salary')
print(salary, employee)

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