Sets: How a Single Rule Can Affect Efficiency

Let’s explore this other data structure: the set. A set is a data structure that does not allow duplicate values to be contained within it.

Sets are of different types, but for this discussion, I’ll talk about an array-based set. This set is just like an array—it’s a simple list of values. The only difference between this set and a classic array is that the set never allows duplicate values to be inserted into it.

For example, if you had the set ["a", "b", "c"] and tried to add another "b", the computer just wouldn’t allow it, since a "b" already exists within the set.

Sets are useful when you need to ensure that you don’t have duplicate data.

For instance, if you’re creating an online phone book, ...

Get A Common-Sense Guide to Data Structures and Algorithms in Python, Volume 1 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.