Lists

If you're coming from another language

Python’s list is closer to Java’s ArrayList or JavaScript’s Array than to a C-style fixed array — it resizes automatically and isn’t restricted to one type:

Try it — edit and run

This flexibility is convenient, but it also means Python won’t catch a type mistake in a list at compile time the way a strictly-typed array would — worth keeping in mind given what we covered about runtime vs. compile-time errors.

Literal syntax and indexing

A list is an ordered, mutable collection — you’ve already seen the syntax used without explanation earlier:

Try it — edit and run

Negative indexing is Python-specific — if you’re coming from Java/C++, there’s no tools[tools.length - 1] needed; -1 just means “last.”

Slicing

list[start:stop] pulls a sub-list — start is inclusive, stop is exclusive:

Try it — edit and run

Mutability and common methods

Lists can be changed in place after creation — this is different from strings, which are immutable in Python (an operation like .upper() returns a new string rather than modifying the original):

Try it — edit and run
Try it — edit and run

.sort() modifies the list in place and returns None; it doesn’t return the sorted list. If you want a sorted copy without touching the original, use sorted(numbers) instead, which returns a new list.

Check your understanding
1/4

Which existing data structure is Python's list closest to?

Exercise · Graded

Sort the scores in descending order, then slice out the top two.