Content · Programming · Python

Python set.add Method: Usage and Examples

set.add(element) inserts one hashable element into an existing set. Adding an equal element again has no effect. See two practical examples, edge cases and common mistakes.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

A check-in system records unique attendee IDs.

What You Need to Understand


set.add(element) inserts one hashable element into an existing set. Adding an equal element again has no effect.

When to Use It


A check-in system records unique attendee IDs.

Step-by-Step Python Example


attendees = {101, 102}
attendees.add(103)
attendees.add(101)
print(sorted(attendees))

How the Example Works


Only 103 is new; the duplicate 101 does not create another entry.

Example Output


[101, 102, 103]
{(1, 2)}

A Second Example


Compare this smaller variation with the main example, then experiment with your own values.

groups = set()
groups.add((1, 2))
print(groups)

Important Behavior and Edge Cases


add() accepts one hashable object, not an entire iterable to unpack. Use update() for multiple members.

Try It Yourself


Try adding a tuple as a single set member.

Common Beginner Mistakes


  • add() accepts one hashable object, not an entire iterable to unpack. Use update() for multiple members.
  • Using the wrong collection method for this operation without checking whether it mutates the object, returns a result, or raises on missing data.

Key Takeaways


  • set.add(element) inserts one hashable element into an existing set.
  • Only 103 is new; the duplicate 101 does not create another entry.
  • add() accepts one hashable object, not an entire iterable to unpack.
Share this resourceSend this page to someone who may find it useful.