
Syntax: List Editing
Adding, updated, and removing list values.
Editing Python Lists
Lists in Python are mutable, meaning you can add, update, and remove values. The append(), insert(), remove(), pop(), and assignment operations can be used for this purpose.
numbers = [1, 2, 3, 4, 5]
# Adding
numbers.append(6) # [1, 2, 3, 4, 5, 6]
numbers.insert(0, 0) # [0, 1, 2, 3, 4, 5, 6]
# Updating
numbers[0] = 100 # [100, 1, 2, 3, 4, 5, 6]
# Removing
numbers.remove(3) # [100, 1, 2, 4, 5, 6]
last = numbers.pop() # 6, and numbers is now [100, 1, 2, 4, 5]
print(numbers)
print(last)
> [100, 1, 2, 4, 5]
> 6
Here are the key takeaways from the examples above:
-
append(element): This method adds an element to the end of the list. It takes one argument: the element you want to add. This operation is performed in-place; it directly modifies the original list. Theappend()method is very useful when you want to dynamically build up a list, for example when processing or collecting data in a loop. -
insert(index, element): This method inserts an element at a specific position in the list, shifting up the existing element at that position and all subsequent elements. The first argument is the index at which to insert, and the second argument is the element you want to insert. Likeappend(), this operation is also performed in-place. -
Assignment operation
numbers[index] = element: This allows you to update a specific element of the list. The element at the given index is replaced with the new element. Note that the index must be a valid index of the list, or aIndexErrorwill be raised. The assignment operation is handy when you have computed a new value that needs to replace an existing value. -
remove(element): This method removes the first occurrence of a value from the list. It takes one argument: the element you want to remove. If the element is not found, it raises aValueError. This method is useful for list cleanup, especially when dealing with data that may have unwanted or duplicate entries. -
pop(index): This method removes the element at the specified index and returns it. If no index is provided, it defaults to -1, which means the last element is removed and returned. If the provided index is out of range, aIndexErrorwill be raised. Thepop()method is useful when you need to use the element you're removing. For instance, you might be moving elements from one list to another, or you need to process and then discard the elements one by one.
Practice: Editing Lists
-
Create a list
ageswith values[20, 25, 30, 35]. -
Append
40to the list. -
Update the first element of the list to
22. -
Remove
30from the list. -
Print the list. It should be
[22, 25, 35, 40].
HINT*: Click here for a hint
> [22, 25, 35, 40]
SOLUTION*: Click here for an example solution
ages = [20, 25, 30, 35]
ages.append(40)
ages[0] = 22
ages.remove(30)
print(ages)
Practice: Popping Items from a List
-
Create a list
temperatureswith values[70, 75, 80, 85, 90]. -
Pop the last element from the list and assign it to
high. -
Pop the first element from the list and assign it to
low. -
Print
low,high, and the list. They should be70,90, and[75, 80, 85]respectively.
HINT*: Click here for a hint
> 70
> 90
> [75, 80, 85]
SOLUTION*: Click here for an example solution
temperatures = [70, 75, 80, 85, 90]
high = temperatures.pop()
low = temperatures.pop(0)
print(low)
print(high)
print(temperatures)
Fantastic job! You have now mastered editing operations in Python lists. This will be particularly useful when preprocessing or cleaning data in Python.