Search⌘ K
AI Features

Lists

Explore the fundamentals of Python lists, including their mutability and typical use cases for storing ordered collections of similar objects. Understand common list methods and when to choose lists over other data structures for efficient and readable code.

Overview

Python’s generic list structure is integrated into a number of language features. We don’t need to import them and rarely need to use method syntax to access their features. We can visit all the items in a list without explicitly requesting an iterator object, and we can construct a list (as with a dictionary) with very simple-looking syntax.

In Python, lists should normally be used when we want to store several instances of the same type of object; lists of strings or lists of numbers. We’ll often use a type hint list[T] to specify the type, T, of an object kept in the list, for example, list[int] or list[str].

Note: Remember that from __future__ import annotations is required for this to work.

Mutability

Lists must be used when we want to store items in some kind of order. Often, this is the order in which they were inserted, but other criteria can also sort them. Lists are mutable, so items can be added, replaced, ...