Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if value is one of a given number of values - set vs. tuple vs. list

Tags:

python

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.

like image 785
luk2302 Avatar asked Dec 17 '19 14:12

luk2302


1 Answers

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

like image 140
C.Nivs Avatar answered Nov 09 '22 18:11

C.Nivs