If foo
is a builtin set
that I know contains "bar"
, which of these is faster? Which is more Pythonic?
foo.add("bar")
or
if "bar" not in foo:
foo.add("bar")
The standard solution to check for existence of an element in the set container ( std::set or std::unordered_set ) is to use its member function find() . If the specified element is found, an iterator to the element is returned; otherwise, an iterator to the end of the container is returned.
To check if the Set contains an element in Python, use the in keyword, which returns True if the specified Set contains an element and False otherwise. The in keyword checks if the item is present in a sequence like a list, range, string, set, etc.
Sets cannot contain duplicates. Duplicates are discarded when initializing a set. If adding an element to a set, and that element is already contained in the set, then the set will not change.
The set is far faster, in general. Testing for membership in a list is O(n), linear in the size of the list. Adding to a set is O(1), independent of the number of the items in the list.
Actually, the second may be faster (output from IPython):
In [2]: %timeit s.add("a")
The slowest run took 68.27 times longer than the fastest. This could mean that an intermediate result is being cached
10000000 loops, best of 3: 73.3 ns per loop
In [3]: %timeit if not "a" in s: s.add("a")
10000000 loops, best of 3: 37.1 ns per loop
But anyway, the first one is more Pythonic, I agree.
The pythonic way is to do first, ask later. Just add it to the set.
Asking first is more common in languages such as C.
Performance is usually not key in python code. Readability is usually much more important, so writing ideomatic code is good practice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With