Altering Lists

The elements of a list can be altered after the list has been created. We can make changes to the elements of a list in the following ways:

  1. Updating elements We can replace the value stored at a particular index with any other value.

  2. Adding elements We can insert new elements into a list at any position that we choose.

  3. Removing elements We can delete any element from a list.

We illustrated each of these concepts below.

# Define the list Fruit.
Fruit = ['apple', 'ornge', 'lemon', 'grap', 'peach']
print(Fruit)
['apple', 'ornge', 'lemon', 'grap', 'peach']

Modifying elements of a list

Two of the elements in our list were misspelled. We will replace these elements with correctly spelled entries.

Fruit[1] = 'orange'
Fruit[3] = 'grape'
print(Fruit)
['apple', 'orange', 'lemon', 'grape', 'peach']

Inserting elements into a list

The insert() list method allows us to add elements to a list at a specified position. Any elements whose previous indices were greater than or equal to the index of the newly inserted element will be pushed back one position.

# Insert plum as the second element of the list.
Fruit.insert(1, 'plum')
print(Fruit)
['apple', 'plum', 'orange', 'lemon', 'grape', 'peach']
# Insert lime as the fifth element of the list.
Fruit.insert(4, 'lime')
print(Fruit)
['apple', 'plum', 'orange', 'lemon', 'lime', 'grape', 'peach']

Appending elements to a list

The append() method is similar to insert(), except that no position is specified. Appended elements are always added to the end of the list.

# Add strawberry and raspberry to the end of the Fruit list.
Fruit.append('strawberry')
Fruit.append('raspberry')
print(Fruit)
['apple', 'plum', 'orange', 'lemon', 'lime', 'grape', 'peach', 'strawberry', 'raspberry']

Deleting list elements by index

We may use the del keyword to delete the element of a list at a certain index.

# Delete the third element of the Fruit list.
del Fruit[2]
print(Fruit)
['apple', 'plum', 'lemon', 'lime', 'grape', 'peach', 'strawberry', 'raspberry']

Keep in mind that once an element is deleted (or inserted), all later elements will have their indices updated.

# Delete the first two elements of the Fruit list.
del Fruit[0]
del Fruit[0]
print(Fruit)
['lemon', 'lime', 'grape', 'peach', 'strawberry', 'raspberry']

Deleting list elements by value

The remove() method will search through a list and remove the first element that matches the supplied argument. If there are multiple elements equal to the given argument, only the one with the lowest index will be deleted.

# Remove the first instance of peach from the list.
Fruit.remove('peach')
print(Fruit)
['lemon', 'lime', 'grape', 'strawberry', 'raspberry']
# Add a second lime element to the end of the list.
Fruit.append('lime')
print(Fruit)
['lemon', 'lime', 'grape', 'strawberry', 'raspberry', 'lime']
# Remove the first instance of lime from the list. 
Fruit.remove('lime')
print(Fruit)
['lemon', 'grape', 'strawberry', 'raspberry', 'lime']

Example: Altering Lists

A list of integers is defined in the next cell. Perform the requested tasks on this list.

intList = [3, 11, 45, 14, 47, 18, 42, 41, 20, 13, 33, 6]

Update the element whose value is 18 to have a value of 28. Print intList.

intList[5] = 28
print(intList)
[3, 11, 45, 14, 47, 28, 42, 41, 20, 13, 33, 6]

Insert an element with a value of 42 between the elements whose values are 20 and 13. Print intList.

intList.insert(9, 42)
print(intList)
[3, 11, 45, 14, 47, 28, 42, 41, 20, 42, 13, 33, 6]

Use the append() method to add an element with a value of 34 to the end of the list. Print intList.

intList.append(34)
print(intList)
[3, 11, 45, 14, 47, 28, 42, 41, 20, 42, 13, 33, 6, 34]

Use the remove() method to delete the first occurence of 42 in the list. Print intList.

intList.remove(42)
print(intList)
[3, 11, 45, 14, 47, 28, 41, 20, 42, 13, 33, 6, 34]

Use the del keyword to delete the first three elements of intList. Print intList.

del intList[0]
del intList[0]
del intList[0]
print(intList)
[14, 47, 28, 41, 20, 42, 13, 33, 6, 34]

The output of the last cell should be: [14, 47, 28, 41, 20, 42, 13, 33, 6, 34]