5.2.4 Popping & Clearing: pop() and clear()

Unit: Unit 5: Dictionaries and Sets • Interactive Practice

Quick Answer & Summary:

Interactive Python programming tutorial and practice in your browser.

Interactive Challenge (Run Live in Browser):

# Practice set pop() and clear()
worker_tasks = {"task_audit", "task_backup"}

# 1. Pop an arbitrary element from the set
popped_task = worker_tasks.pop()
is_valid_task = popped_task in ["task_audit", "task_backup"]

# 2. Clear all remaining items from the set
worker_tasks.clear()

print("Task successfully popped:", is_valid_task)
print("Set after clear:", worker_tasks)
print("Length after clear:", len(worker_tasks))
Open in Live Interactive Python Sandbox →