Python 101Free
DATA STRUCTURES

Lists

Ordered, mutable sequences, the everyday container.

SECTION 01

The list model

When you need a sequence you can grow, shrink, and rearrange on the fly, a list is the right container. Order is preserved, duplicates are allowed, and elements can be of mixed types (although mixed-type lists are usually a code smell).

Indexing and length both work in O(1). Appending to the end is also O(1) amortized, which is why append is the workhorse of list construction. Inserting in the middle requires shifting everything to the right, which is O(n).

Lists in Python feel like dynamic arrays in any language. The behavior, the costs, and the syntax are all close enough to what you would expect that the only thing to internalize is that the bracket form is the standard syntax.

2 more sections

Create a free account to access all sections and animations.

Create free accountAlready have an account? Sign in