Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Set basics
7. Deleting elements – remove()
Relationships between sets
Summary

Instruction

Very well done! Instead of discard(), you can use remove() to delete information. The difference is that remove() throws a KeyError when a given element is not present in the list. Take a look:

newcomers = {'Kathleen Lopez', 'Clinton Keller', 'Katie Gutierrez', 'Pablo Fleming'}
try:
  newcomers.remove('Strange Name')
  print('Removed successfully!')
except KeyError:
  print('Employee not found!')

In the code above, we tried to remove a person called 'Strange Name' from the set. Such a person does not exist in the set, so we'll get a KeyError. We handle this error by printing 'Employee not found!'.

Exercise

As terrible as it may sound, we need a way of firing people from the new branch. Add some new code to the template that will do the following:

  1. Ask the user to provide an employee {x}:

    Which employee would you like to fire? 
  2. If {x} exists in the set, remove it and print:

    {x} was fired.

    Otherwise, print:

    The person named {x} does not work for us!