3.6.5. Modifying lists#

This section shows how to modify one or many elements of a list (using indexing and slicing), how to extend lists or append data to lists, and how to remove an element from a list using pop.

Important

Since tuples are immutable, then this page does not apply to tuples.

3.6.5.1. Modifying an element#

We already know how to read one element of a list using indexing, and how to read many elements of a list using slicing:

a = [1, 2, 3]

# Read the first element (indexing)
print(a[0])

# Read the first and second elements (slicing)
print(a[0:2])
1
[1, 2]

We can also use indexing and slicing to write elements of a list.

To write one element using indexing:

a = [1, 2, 3]
a[0] = 10

a
[10, 2, 3]

To write multiple elements at once using slicing:

a = [1, 2, 3]

# Assign 20 and 30 from `b` in place of 1 and 2 in `a`
a[0:2] = [10, 20]

a
[10, 20, 3]

3.6.5.2. Appending an element to a list#

To append a new element to a list, we use the list’s append method, which takes the new value as an argument.

Function vs method

A function is a subprogram that can process arguments and return a result.

output = function(variable)

A method is a special type of function that is only available for a given variable type. It is called using the variable itself, using the dot . operator:

output = variable.method()

Methods sometimes modify the variable itself, which is the case with append and extend.

a = [1, 2, 3]
print("a before =", a)

a.append(4)

print("a after =", a)
a before = [1, 2, 3]
a after = [1, 2, 3, 4]

3.6.5.3. Extending a list#

To append multiple elements at once, we instead use the lists’ extend method, which takes a list of new values as an argument.

a = [1, 2, 3]
print("a before =", a)

a.extend([4, 5])

print("a after =", a)
a before = [1, 2, 3]
a after = [1, 2, 3, 4, 5]

3.6.5.4. Deleting an element from a list#

To remove an element at a given index, we use the list’s pop method, which both returns the element being deleted and deletes it from the list.

a = [1, 2, 3]

print(a.pop(0))  # Remove the first element
print(a)
1
[2, 3]