About 6513 letters

About 33 minutes

#Python's list

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"]
List: List: Index: Index: List:->Index: values Tom Jerry Spike indices 0 1 2

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.

#Indexing

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)

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

#Adding Elements

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)

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

#Removing Elements

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)

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

#Clearing a List

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)

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

#Sorting

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)

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

#Reference Type

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:

G data Value [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] numbers Variable numbers numbers->data Reference shadow Variable shadow shadow->data Reference
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)

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

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:

G cluster_1 After cluster_0 Before svalue Value 11 nvalue Value 10 shadow2 Variable shadow shadow2->svalue Reference number2 Variable number number2->nvalue Reference value Value 10 number Variable number number->value Reference shadow Variable shadow shadow->value Reference
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

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

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)

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

#len Function

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]))

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

Created in 5/15/2025

Updated in 5/21/2025