August 2019
Beginner
482 pages
12h 56m
English
Sets are—in a way—dictionaries without values. First, they use the same curly brackets, and second, their members cannot be duplicated, which are both similar to dictionary keys. Because of that, they are handy to use for deduplication or membership tests. On top of that, sets have built-in mathematical operations, unions, intersections, differences, and symmetrical differences:
>>> names = set(['Sam', 'John', 'James', 'Sam'])>>> names{'James', 'John', 'Sam'}>>> other_names = {'James', 'Nikolai', 'Iliah'}>>> names.difference(other_names){'John', 'Sam'}>>> names.symmetric_difference(other_names){'Iliah', 'John', 'Nikolai', 'Sam'}
Sets don't have an order and, compared to dictionaries, do not guarantee that the order of representation ...