Lists: Exercises


2. Modifying Lists. In Python, you can modify a list by changing, adding, or removing items. To change an item, use indexing and assignment. To add items, you can use methods like append() and extend(). To remove items, you can use methods like pop() and remove().

For example, you can change the second item in a list like this: numbers[1] = 5.

Start by changing the value of the third item in the list numbers to 8.

Add the number 9 to the end of the list using append().


# Define a list of numbers. numbers = [6, 7, 9] # Set the third element in the list equal to 9. numbers[2] = ___ # Add the number 10 to the end of the list. numbers.___ # Print the modified list print(numbers) # Define a list of numbers. numbers = [6, 7, 8] # Set the third element in the list equal to 8. numbers[2] = 8 # Add the number 9 to the end of the list. numbers.append(9) # Print the modified list print(numbers) test_object("numbers") success_msg("You have correctly modified the list `numbers`.")
To modify a list, use indexing for changes, and methods like append() and pop() for additions and removals.

Previous Exercise Next Exercise