Set Discard in Python

Level 250 keystrokes

This removes an element from a set using discard(). Unlike remove(), it doesn't raise an error if the element doesn't exist.

Python code

# Create a set with five numbers
1nums = {1, 2, 3, 4, 5}
# discard() removes the element if it exists
# No error if element is not found
2nums.discard(3)
# Print the set without 3
3print(nums)

Output

{1, 2, 4, 5}
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