Storing Data Using Sets
A set is an unordered collection of distinct items. Unordered means that items aren’t stored in any particular order. Something is either in the set or it’s not, but there’s no notion of it being the first, second, or last item. Distinct means that any item appears in a set at most once; in other words, there are no duplicates.
Python has a type called set that allows us to store mutable collections of unordered, distinct items. (Remember that a mutable object means one that you can modify.) Here we create a set containing these vowels:
| >>> vowels = {'a', 'e', 'i', 'o', 'u'} |
| >>> vowels |
| {'a', 'u', 'o', 'i', 'e'} |
It looks much like a list, except that sets use braces ...
Get Practical Programming, 2nd Edition 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.