From the course: Programming Foundations: Data Structures (2023)

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

When to use sets

When to use sets

- [Instructor] As a non-linear data structure, sets can have pros and cons, depending on how you use them. Sets are designed to store unique elements, so if you use them in this way, you'll feel their benefits more fully. Sets automatically eliminate duplicate values, making it easier to check for distinctiveness. The main operation that sets provide is membership testing, and it's super fast at O of 1 or Constant Time. However, if you don't care about membership, this data structure has many disadvantages. The sets are unordered, which means if you need your elements ordered, you should probably use another data structure , such as a list or a tuple. Sets also lack indexing. You cannot access elements in a set by their position because they're unordered. So while sets provide unique advantages for membership checking, they do have limited data manipulation options.

Contents