Python List

Beginner
9 min read
Updated: Nov 2026
3 examples
Ordered sequences

Definition and Usage

A list stores items in order, like a numbered to-do list. Each item has a position, called an index, starting at 0, and the list can grow, shrink, and change after you create it.

A list is ordered, mutable, allows duplicates, and can mix types. The List Methods index lists every guide, such as append(), pop(), and sort().

Python Syntax

Square brackets hold items separated by commas; square brackets after a list read one item by index or several by slice:

Python
fruits = ["apple", "banana", "cherry"]  # a list literal
fruits.append("date")                    # add to the end
print(fruits[0], fruits[-1])             # apple date
print(fruits[1:3])                       # ['banana', 'cherry']
print(len(fruits), "kiwi" in fruits)     # 4 False
for i, fruit in enumerate(fruits):
    print(i, fruit)
Slices never raise.

fruits[1:3] means "from index 1 up to, but not including, 3" and returns a new list. A slice past the end, such as fruits[2:99], just stops at the end, while a single index past the end raises IndexError.

How Lists Behave

RuleWhat it means
OrderedItems keep the position you gave them. Printing and looping always follow that order, and two lists are equal only if they have the same items in the same order.
MutableYou can add, remove, replace, and reorder items after the list is created. Two names bound to the same list see the same changes.
Duplicates allowedThe same value can appear many times: [1, 1, 1] is a valid list of three items.
Mixed typesOne list can hold numbers, strings, None, and other lists or dicts, such as [1, "two", 3.0, None].
Zero-based indexesThe first item is lst[0] and the last is lst[len(lst) - 1]. Reading a position that does not exist raises IndexError.
Negative indexeslst[-1] is the last item and lst[-2] the one before it, so you never need to compute the length yourself.

Creating Lists

CodeWhen to use it
[2, 3, 5, 7]Literal: the most common way.
list("hey")From any iterable; a string gives one item per character.
list(range(0, 10, 2))A run of numbers from range().
[n * n for n in nums]List comprehension: compute each item.
"red green blue".split()Split a string into a list of words.
[0] * 5Repeat one immutable value; never use this with a list inside.

The list() constructor turns any iterable into a list, and range() produces evenly spaced numbers. An empty list is [] or list().

Looping Over a List

LoopWhat you get
for item in lst:Each item, front to back.
for i, item in enumerate(lst):The position and the item together.
for item in reversed(lst):Back to front, without copying the list.
for a, b in zip(lst, other):Two lists side by side, stopping at the shorter one.
while lst:Keep going until the list is empty, usually with lst.pop() inside.

enumerate() is the clean way to get positions; avoid for i in range(len(lst)) unless you really need only the index. zip() pairs items from two lists.

Common Mistakes

MistakeDo this instead
x = lst.sort()sort() returns None, so x is None. Call lst.sort() on its own line, or use x = sorted(lst).
[[]] * 3 for a gridAll three rows are one shared inner list (aliasing), so appending to one row changes them all. Use [[] for _ in range(3)].
Removing items while looping over the same listEach removal shifts the later items left, so the loop skips the next one. Loop over a copy, lst[:], or build a new list with a comprehension.
lst[len(lst)]Raises IndexError: the last valid index is len(lst) - 1, or simply lst[-1].
append() vs extend()a.append([1, 2]) adds one nested list; a.extend([1, 2]) adds two items. a.extend("hi") adds the letters h and i.
b = a to copyBoth names point to one list. Use b = a.copy() or a[:], or copy.deepcopy(a) for nested lists.

List vs Tuple vs Set vs Dict

TypeLiteralBest forContentsChange it?
list[1, 2, 1]Ordered items you will changeDuplicates allowedMutable
tuple(1, 2, 1)Fixed records, dict keysDuplicates allowedImmutable
set{1, 2}Fast membership tests, removing duplicatesUnique, hashable items; no orderMutable
dict{"a": 1}Look up values by keyUnique keys, insertion orderMutable

Read more about key/value lookups on the Python Dict page.

Python Examples

Fixed data only, so the output is the same on every machine.

Example 1 — Create Lists Six Different Ways

A literal, list() on a string, range(), a comprehension, split(), and a list that mixes types.

Python
primes = [2, 3, 5, 7]
letters = list("hey")
evens = list(range(0, 10, 2))
squares = [n * n for n in range(1, 6)]
words = "red green blue".split()
mixed = [1, "two", 3.0, None, [5]]

print(primes, letters)
print(evens, squares)
print(words)
print(mixed, len(mixed))

How It Works

  1. Literal and constructor. Square brackets list the items directly, while list() turns a string into one item per character.
  2. Numbers on demand. range(0, 10, 2) counts from 0 up to, but not including, 10 in steps of 2; the comprehension squares 1 through 5.
  3. From text. split() with no argument breaks the string at whitespace into a list of words.
  4. Any types together. One list can hold an int, a str, a float, None, and even another list, which counts as a single item.

Example 2 — Read a Week of Days by Index and Slice

Positive and negative indexes, slices with a step, and what happens one step past the end.

Python
week = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]

print(week[0], week[-1], week[-2])
print(week[1:4])
print(week[5:], week[::2])
print(week[::-1][:3])
try:
    print(week[7])
except IndexError:
    print("week[7] is out of range: valid indexes are 0 to 6")

How It Works

  1. Both ends. Index 0 is Monday, and the negative indexes -1 and -2 count back from Sunday.
  2. Stop is excluded. week[1:4] takes positions 1, 2, and 3, so Thursday is the last day included.
  3. Open ends and steps. week[5:] runs to the end, week[::2] takes every second day, and [::-1] reverses before [:3] keeps three days.
  4. One past the end. Seven days means indexes 0 to 6, so week[7] raises IndexError, while a slice would just stop at the end.

Example 3 — Hit Three Classic List Mistakes on Purpose

A grid built with *, a "copy" made with =, and the result of sort() stored in a variable.

Python
rows = [[]] * 3
rows[0].append("x")
print(rows)

rows = [[] for _ in range(3)]
rows[0].append("x")
print(rows)

original = [3, 1, 2]
same = original
same.append(0)
print(original)

result = original.sort()
print(result, original)

How It Works

  1. One inner list, three times. [[]] * 3 repeats a reference to a single empty list, so appending to row 0 shows up in every row.
  2. Separate rows. The comprehension runs [] three times and creates three different lists, so only the first row changes.
  3. = is not a copy. same is another name for original, so appending 0 through it changes the original; use copy() instead.
  4. In-place methods return None. sort() sorts original and returns None, which is what result holds.

Next: Python List Methods by Group

GroupMethods
Add itemsappend(), insert(), extend()
Remove itemspop(), remove(), clear()
Search and countindex(), count()
Order and copysort(), reverse(), copy()

Frequently Asked Questions

A list is a built-in, ordered, mutable sequence of items written in square brackets, such as [3, "a", None]. You read items by position, starting at 0.
Use append(x) to add one item at the end, insert(i, x) to add one item at a position, and extend(iterable) or += to add many items at the end.
It counts from the end: lst[-1] is the last item, lst[-2] the second to last. lst[-len(lst)] is the first item, and anything further raises IndexError.
Use b = a.copy(), b = a[:], or b = list(a) for a shallow copy. Plain b = a only creates a second name, and nested lists need copy.deepcopy(a).
Use a tuple for a fixed group of values that should not change, such as coordinates or a record used as a dict key. Use a list when you will add, remove, or reorder items.

Browse every list method

Open the method index for categorized guides with examples and FAQs.

List Methods Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful