About 6513 letters
About 33 minutes
A list is an ordered collection of mutable values.
The literal syntax for a list uses square brackets []
to wrap a group of values. For example:
students: list[str] = ["Tom", "Jerry", "Spike"]
The type annotation list[str] indicates a list where each element is of type str
.
List elements can have different types, for example:
ages: list[int | str] = [15, "16", 17]
The type annotation list[int | str] indicates a list whose elements are either int
or str
.
Unless you have a good reason, avoid using lists with mixed element types.
Just like with tuples, you can access list elements by using square bracket []
indexing.
Indexing starts from 0, and list elements can be modified:
students: list[str] = ["Tom", "Jerry", "Spike"]
print(students)
print(students[0])
print(students[1])
print(students[2])
students[1] = "Tuffy"
print(students)
Python provides several ways to add elements to a list, mainly including append
, insert
, and extend
:
append
— Appends an element at the end insert
— Inserts an element at a specified position extend
— Merges another list into the end Example:
numbers: list[int] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
numbers.append(10) # Append 10 at the end
numbers.insert(2, 233) # Insert 233 at index 2
numbers.insert(233, 666) # Index 233 is out of range, so 666 is appended at the end
print(numbers)
Python provides several ways to remove elements from a list, mainly including pop
and remove
:
pop
— Removes the element at the specified index; removes the last element if no index is given remove
— Removes the first element with the specified value Example:
numbers: list[int] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
numbers.pop() # Remove last element
numbers.pop(2) # Remove element at index 2
numbers.remove(7) # Remove the first occurrence of value 7
print(numbers)
You can clear a list using the clear
method:
numbers: list[int] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
numbers.clear() # Clear the list
print(numbers)
You can sort a list using the sort
method:
numbers: list[int] = [5, 4, 7, 0, 1, 3, 6, 2, 8, 9]
numbers.sort() # Sort in ascending order
print(numbers)
numbers.sort(reverse=True) # Sort in descending order
print(numbers)
In Python, all types are reference types. When a variable is assigned to another, they both reference the same underlying object. See Python Official Docs - Mutable Sequences
For lists, modifying the list through any variable affects all references to that list:
numbers: list[int] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # Create list [0, ..., 9] and assign to variable numbers
shadow: list[int] = numbers # Assign numbers to shadow, both refer to the same list
numbers[1] = 233 # Modify list through numbers
shadow[2] = 666 # Modify list through shadow
print(numbers) # Both numbers and shadow reflect changes
print(shadow)
Previously learned primitive types and tuples are immutable. Modifying them actually creates a new object. See Python Official Docs - Immutable Sequences
So, objects that reference the same value will not affect each other:
number: int = 10 # Create an integer 10 and assign to variable number
shadow: int = number # Assign number to shadow; both refer to the same value 10
number = 11 # Assigning 11 to number creates a new integer; does not affect shadow
print(number) # number references 11
print(shadow) # shadow still references original 10
To make two lists independent, you need to create a new list:
numbers: list[int] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # Create list [0, ..., 9]
shadow: list[int] = list(numbers) # Create a new list based on numbers and assign to shadow
numbers[1] = 233 # Modify via numbers
shadow[2] = 666 # Modify via shadow
print(numbers)
print(shadow)
The built-in len
function can be used to get the length of a tuple or list.
Example:
numbers_tuple: tuple[int, ...] = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
numbers_list: list[int] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(len(numbers_tuple))
print(len(numbers_list))
print(len(numbers_list[2:7]))
Created in 5/15/2025
Updated in 5/21/2025