Introduction to Python Resources

Begin your free Python journey now,
at your own pace

check

Syntax: List Editing

check

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. The append() 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. Like append(), 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 a IndexError will 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 a ValueError. 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, a IndexError will be raised. The pop() 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

  1. Create a list ages with values [20, 25, 30, 35].

  2. Append 40 to the list.

  3. Update the first element of the list to 22.

  4. Remove 30 from the list.

  5. Print the list. It should be [22, 25, 35, 40].

HINT*: Click here for a hint
*Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.
> [22, 25, 35, 40]
SOLUTION*: Click here for an example solution
*Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
ages = [20, 25, 30, 35]
ages.append(40)
ages[0] = 22
ages.remove(30)
print(ages)

Practice: Popping Items from a List

  1. Create a list temperatures with values [70, 75, 80, 85, 90].

  2. Pop the last element from the list and assign it to high.

  3. Pop the first element from the list and assign it to low.

  4. Print low, high, and the list. They should be 70, 90, and [75, 80, 85] respectively.

HINT*: Click here for a hint
*Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.
> 70
> 90
> [75, 80, 85]
SOLUTION*: Click here for an example solution
*Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
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.