I have a value
and want to check if that value is (for example) either '5'
, 'test'
or '#+*'
.
As far as I can tell I have a couple of options:
value in ['5', 'test', '#+*'] # list
value in ('5', 'test', '#+*') # tuple
value in {'5', 'test', '#+*'} # set
Are those three statements equivalent or is there some difference?
I am not really concerned about performance since the check will always compare against < 10 elements.
There is a difference. First, the mutable data structures list
and set
will be larger in size, since they are growable and need to have associated overhead with them. tuple
is immutable, and thus smaller in memory.
Second, checking for membership in a tuple
or list
is an O(N) operation, that is, it depends on the size of the data structure, since it must iterate from start to either the desired element or the end of the structure, whichever comes first. The set
doesn't need to do this, since it's checking a hash and the lookup doesn't depend on the size of the set
.
The pythonic way? It depends. If you are doing this test in a loop, then a set
would make more sense since the time difference even for a small number of elements is noticable:
❰mm92400❙~❱✔≻ python -m timeit -s 'x = list(range(9))' '8 in x'
10000000 loops, best of 3: 0.0979 usec per loop
❰mm92400❙~❱✔≻ python -m timeit -s 'x = tuple(range(9))' '8 in x'
10000000 loops, best of 3: 0.0968 usec per loop
❰mm92400❙~❱✔≻ python -m timeit -s 'x = set(range(9))' '8 in x'
10000000 loops, best of 3: 0.0278 usec per loop
Otherwise, for just a one-off, a tuple
is smaller in memory, so I might go that route
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