QamoosTech
Programming FundamentalsData structuresBeginnerPython

Set

PronunciationSET

Definition

A set is an unordered collection of unique values. It is fast at checking whether something is inside and at removing duplicates.

Where you hear it

In Python code that removes duplicates, compares two groups of items, or checks membership many times.

Examples

  • Convert the list to a set to remove the duplicate emails.
  • The intersection of the two sets gives the users who are in both groups.
  • A set of user IDs makes it fast to check whether a user has already voted.

Common mistake

Expecting a set to keep order or hold duplicates. It does neither, and {} creates an empty dictionary, not an empty set.

Don't confuse with

A list, which keeps order and allows duplicates. Use a set when uniqueness and fast lookup matter more.

Say it at work

  • In a meeting or chatPut the ids in a set so the membership check is instant.
  • In an email or ticketA set will drop the duplicates for us.