list.pop()

Python · Lists

Theory

.pop() removes the last item of a list and returns it, so you can remove and use a value in one step. sales = [100, 250, 300] then last = sales.pop() leaves last = 300 and sales = [100, 250]. Pass an index to remove a specific position instead: sales.pop(0) removes and returns the first item. Calling pop() on an empty list, or with an index that does not exist, raises IndexError.

Popping from the end is O(1), since nothing has to move, which makes append() plus pop() a perfect stack: last in, first out, like a pile of plates or an undo history. Popping from the front, pop(0), is O(n) because all remaining items shift left; for a first-in, first-out queue use collections.deque and its popleft(). Unlike remove(), pop() works by position, and unlike del, it gives you the removed value back.

Why we use it

Many algorithms process items one at a time while taking them out of a collection: undo stacks, depth-first search, work lists. pop() removes the item and hands it to you in a single step.

When to use it

Use pop() to take the last item (stack behaviour) or pop(i) when you need the value at a position. Use del list[i] if you do not need the value, remove() to delete by value, and deque.popleft() for queues.

How it works

  1. Call sales.pop()
  2. Is the list empty? Yes: Raise IndexError. No: Take the item at the last index.
  3. Remove that slot and shrink the list by 1
  4. Return the removed item to the caller
  5. print(last, sales) shows 300 [100, 250]

Syntax

list.pop()
list.pop(index)

Example

sales = [100, 250, 300]
last = sales.pop()
print(last, sales)

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