list.insert()

Python · Lists

Theory

.insert(index, item) puts item into the list so that it ends up at position index, shifting the item that was there, and everything after it, one place to the right. sales = [100, 250, 300] then sales.insert(1, 999) gives [100, 999, 250, 300]. Like other list methods it changes the list in place and returns None.

Out-of-range indexes do not raise errors: an index of len(list) or larger appends at the end, and a very negative index inserts at the front. Negative indexes count from the end, but insert(-1, x) places x before the current last item, not after it. Because every later reference must move, insert() costs O(n): cheap at the end, expensive at index 0 for a long list. If you repeatedly add to the front, collections.deque with appendleft() is built for that.

Why we use it

Sometimes order matters and the new item belongs in the middle: a priority entry at the top, a header row at index 0, or a value placed into an already sorted list. insert() puts it exactly where you say.

When to use it

Use insert() for occasional placement at a specific position. Use append() for adding at the end, which is faster. For many front insertions use collections.deque, and to keep a list sorted as you insert use the bisect module.

How it works

  1. Call sales.insert(1, 999)
  2. Is the index beyond the end? Yes: Treat it as an append at the end. No: Use the given position.
  3. Shift every item from that position one slot to the right
  4. Write 999 into the freed slot
  5. print(sales) shows [100, 999, 250, 300]

Syntax

list.insert(index, item)

Example

sales = [100, 250, 300]
sales.insert(1, 999)
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