Why is x in big_set usually much faster than x in big_list?
Sets and dicts are hash tables. Converting a list to a set pays off when you test membership many times.
Sets · Exercise 1 of 4
Fifteen multiple-choice questions on Python sets: creation, uniqueness, order, remove vs discard, set operations, subsets, hashable elements and frozenset. Pick an option to check your answer instantly.
Why is x in big_set usually much faster than x in big_list?
Sets and dicts are hash tables. Converting a list to a set pays off when you test membership many times.
What is {1, 2} < {1, 2}?
{1, 2} <= {1, 2} is True (subset), but < requires the sets to differ.
After a = {1} and a.union({2}), what is a?
union() does not modify a. a.update({2}) or a |= {2} changes it in place.
Which statement about the order of a set's elements is correct?
String hashes are randomised per process. Use sorted(s) for a predictable order, or dict.fromkeys() to keep insertion order.
Which expression is a set comprehension?
Braces with a single expression make a set; with key: value they make a dict; square brackets a list; round brackets a generator.
Timed online tests with instant scores and explanations.