Technology Lists | Python Tutorial

Learn with diagrams, code, systems and practical examples.

</>
Tutorials›Python›Lists
Data Structures · Chapter 10

Lists

Store an ordered, changeable collection of values.

Why this matters

Lists are a natural choice when order matters and you may add, remove, or update items.

Start with the idea

A list holds values in order. Positions are zero-based indexes, and common operations include append, remove, slicing, and iteration.

IdeaUnderstand the purpose
→
CodeRun the smallest example
→
PracticeChange it yourself
SubjectVision learning pattern: understand the idea before memorizing syntax.

Small working example

Run this example first. Do not change several things at once; confirm the basic behavior, then experiment.

Python
tasks = ["email", "deploy"]
tasks.append("review")
print(tasks[0])
print(tasks)
Expected output
email
['email', 'deploy', 'review']
What to notice

The example is intentionally small. Focus on the chapter’s main idea before adding extra syntax or framework code.

Common beginner mistake

Mistake

Using an index without checking whether the list has that position.

Better approach

Prefer iteration when you need every item, and validate indexes when positions come from external input.

Quick recap

  • Lists preserve order.
  • Lists are mutable.
  • Indexes start at zero.

Try it yourself

These are deliberately small. If you can complete them without copying the example, you are ready to continue.

  1. Add and remove an item.
  2. Print the first and last items safely.
Deep chapter · Learn → Practice → Prepare → Test

Build a reliable mental model for Python lists

A Python list is an ordered, mutable container. In plain language: it keeps items in a sequence, lets you reach items by position, and lets you change the sequence later. Before memorizing methods, ask two questions: which list object am I changing? and does this operation mutate that list or create a new value?

Create, read, update, slice, and ask membership questions

cities = ["Surrey", "Vancouver", "Burnaby"]
print(cities[0])
print(cities[-1])
print(cities[0:2])
print("Vancouver" in cities)

cities[1] = "Richmond"
print(cities)

Expected output: Surrey; Burnaby; ['Surrey', 'Vancouver']; True; then ['Surrey', 'Richmond', 'Burnaby'].

Indexcities[0]

Zero-based positions count from the start.

Negative indexcities[-1]

Negative positions count backward from the end.

Slicecities[0:2]

The stop boundary is excluded.

Membership"Surrey" in cities

Answers whether an equal item is present.

KuveSu original Python list index and slice boundary map Four list items are shown with positive indexes zero through three above, negative indexes minus four through minus one below, and a slice from index one up to but not including index three. positive index negative index A0-4 B1-3 C2-2 D3-1 items[1:3] → B, C · stop 3 excluded
KuveSu original reference visual: indexes point to items; slice boundaries describe a range whose stop is excluded.

Choose methods by the change you intend

OperationIntentImportant result
append(x)Add one item at the end.Mutates the list.
extend(items)Add every item from an iterable.Mutates the list.
insert(i, x)Insert one item before position i.Mutates the list.
remove(x)Remove the first equal value.Raises ValueError if absent.
pop(i)Remove and return an item, defaulting to the last.Mutates and returns a value.
clear()Remove all items.Mutates the list.
sort()Sort the same list in place.Returns None.
sorted(items)Produce a new sorted list from an iterable.Leaves the original list unchanged.
Interactive reference

List State Explorer: mutation or new value?

Choose a deterministic case, predict the result first, then reveal the state transition. Nothing is saved.

Ready. Predict what changes and whether the original list is mutated.
Starting state
—
Operation
—
Resulting state / value
Hidden until reveal
Mutation?
Hidden until reveal
Key idea
Predict first.

Common beginner mistakes and fixes

MistakeWhy it happensFix
Reading items[len(items)]The last valid positive index is one less than the length.Use items[-1] or check the boundary before indexing.
Using append([3, 4]) when you meant two itemsappend adds its argument as one item.Use extend([3, 4]) when you intend to add each element.
items = items.sort()list.sort() changes the list in place and returns None.Call items.sort() separately, or assign sorted(items).
Removing items from the same list while iterating over itIndexes shift while the loop is deciding what comes next.Build a filtered list, iterate over a copy, or choose another explicit strategy.
b = a then expecting independent listsBoth names refer to the same list object.Use a.copy() or a slice for a shallow copy when that is the intended level of copying.
Assuming a shallow copy duplicates nested objects tooThe outer container is copied; nested references are still shared.Model the nested structure explicitly and use deeper copying only when the data semantics require it.

Practice with prediction before execution

  1. Start with [10, 20, 30]. Predict items[-2] and items[1:3], then verify.
  2. Explain in one sentence why append([40, 50]) and extend([40, 50]) produce different shapes.
  3. Create an alias and a shallow copy of the same list. Mutate each and explain which names see which change.
  4. Sort a list once with sort() and once with sorted(). Record the returned value and the final original list in each case.
  5. Rewrite code that removes even numbers while iterating so it produces a new filtered list instead.

Continue: Practice → Prepare → Test

Lists are a foundation for later tuples, sets, comprehensions, functions, iteration, and many interview problems. Use the explorer until you can explain mutation and aliasing without guessing.

The current public Python assessment still serves exactly 25 distinct randomized questions from its validated bank. List-specific candidates remain preparation-only until duplicate and editorial review is complete.

Primary references

List semantics on this page are anchored to the official Python documentation. The visual and List State Explorer are original KuveSu learning aids.