Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I check if an item is already in a set before adding it?

Tags:

python

set

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")
like image 590
javanix Avatar asked Apr 28 '15 19:04

javanix


People also ask

How do you check if an item exists in a set?

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.

How do you check if something is already in a set Python?

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.

What happens if you try add duplicate value in set python?

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.

Is set add faster than list append?

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.


2 Answers

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.

like image 100
honza_p Avatar answered Oct 24 '22 23:10

honza_p


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.

like image 39
Filip Haglund Avatar answered Oct 24 '22 23:10

Filip Haglund