list.extend()

Python · Lists

Theory

.extend(iterable) loops over another iterable and appends each of its items to the end of the list, in place. sales = [100, 250] then sales.extend([300, 400]) gives [100, 250, 300, 400]. It accepts any iterable: a list, tuple, set, range or even a string, which is a common surprise because a string is iterated character by character, so nums.extend('ab') adds 'a' and 'b' separately.

The difference from append() is the key thing to learn. append() adds its argument as one single element, so appending [300, 400] gives a nested list of length 3; extend() unpacks it, giving length 4. sales += [300, 400] does the same in-place extend. In contrast, sales + [300, 400] builds a brand-new list and leaves the original unchanged. extend() returns None, and costs O(k) where k is the number of items added.

Why we use it

Combining batches of data is very common: rows from several files, pages of API results, results from several loops. extend() merges them into one flat list without writing a loop yourself.

When to use it

Use extend() (or +=) to add all items of another iterable to an existing list. Use append() to add one item, even if it is a list you want nested. Use a + b when you need a new combined list and must keep both originals.

How it works

  1. Call sales.extend([300, 400])
  2. Get an iterator over the argument
  3. Take the next item from it
  4. Append that single item to the end of sales
  5. More items in the iterable? Yes: Take the next one. No: Return None.
  6. print(sales) shows [100, 250, 300, 400]

Syntax

list.extend(iterable)

Example

sales = [100, 250]
sales.extend([300, 400])
print(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