List Remove in Python

Level 249 keystrokes

This removes the first occurrence of a value from a list. The remove() method modifies the original list.

Python code

# Create a list in which the value 2 appears twice
1nums = [1, 2, 3, 2, 4]
# remove(2) deletes only the first 2 it finds (at index 1)
# The other 2 stays; a value not in the list would raise ValueError
2nums.remove(2)
# Prints [1, 3, 2, 4]
3print(nums)

Output

[1, 3, 2, 4]
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Python code and how often they appear.

  • ,× 4
  • (× 2
  • )× 2
  • .× 1
  • [× 1
  • ]× 1
  • =× 1

More Python examples at level 2

← Python typing practice