Python Lists

Python · Lists

Theory

A list is an ordered, mutable collection written with square brackets: sales = [100, 250, 300]. Ordered means every item has a position (index) starting at 0, so sales[0] is 100 and sales[-1], counting from the end, is 300. Mutable means you can change it after creation: sales[1] = 999 replaces an item, and append(), insert(), remove() and pop() grow or shrink it. A list can hold mixed types, even other lists.

Under the hood a list is a dynamic array of references to objects. Reading or replacing by index is very fast (O(1)), and append() is fast on average because Python over-allocates spare slots. Inserting or deleting near the front is slow (O(n)) because every later reference must shift. Since a list stores references, b = a does not copy it; both names share one list. Use a.copy() or a[:] for a shallow copy. Also beware [[0] * 3] * 3: it repeats a reference to the same inner list three times.

Why we use it

Most data arrives as a sequence of values: daily sales, customer names, rows from a file. A list keeps them in order, lets you reach any item by position, and can grow as new data comes in.

When to use it

Use a list for ordered data that may change. Use a tuple for fixed records, a set when you only care about unique items or fast membership, and a dict when you look values up by a key. For big numeric arrays, NumPy or pandas are faster.

How it works

  1. sales = [100, 250, 300] creates a list object
  2. Each slot stores a reference to an int object
  3. Accessing by index, e.g. sales[1]? Yes: Jump straight to that slot (fast). No: Use a method like append().
  4. sales.append(400) writes into a spare slot at the end
  5. The same list object is changed in place; no copy is made
  6. print(sales) shows [100, 250, 300, 400]

Syntax

my_list = [item1, item2, item3]

Example

sales = [100, 250, 300]
sales.append(400)

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