3

I want to know what are the best practices between using the in tuple and the in list conditions and know why, as the following scenario:

my_variable = 'A'
if my_variable in [2, 'A', None]:
 return True
elif my_variable in (2, 'A', None):
 return True

And if possible list the advantages/disadvantages of tuples/lists in this case.

asked Apr 29, 2019 at 13:12
5
  • 2
    a set would be the most efficient here... my_variable in set((2, 'A', None)). (but is that really worth it? see comment below...) Commented Apr 29, 2019 at 13:13
  • 2
    That's a pretty pointless optimisation, in my opinion. Such tiny sets/lists/tuples are constructed, searched and destroyed very quickly, so choosing the fastest is not worth it Commented Apr 29, 2019 at 13:15
  • 1
    @ForceBru Yes because containers never contain more than 3 elements ;) Commented Apr 29, 2019 at 13:19
  • @Chris_Rands actually in my case, the containers could be very large, just didn't mention it in the example. Commented Apr 29, 2019 at 13:23
  • 1
    @getName I think you missed my sarcastic tone ;) Commented Apr 29, 2019 at 13:26

3 Answers 3

4

list and tuple both have an O(n) time complexity for x in container checks.

However, set have an O(1) for this check (most of the time, the worst case scenario will have a worse time complexity due to hash collisions).

See these timings for a list, tuple and a set with 1 million elements:

from timeit import Timer
li = list (range(1, 1000000))
t = tuple(range(1, 1000000))
s = set (range(1, 1000000))
def x_in_list():
 999999 in li
def x_in_tuple():
 999999 in t
def x_in_set():
 999999 in s
print(min(Timer(x_in_list).repeat(5, 5)))
print(min(Timer(x_in_tuple).repeat(5, 5)))
print(min(Timer(x_in_set).repeat(5, 5)))

Outputs

0.08769642199999961
0.09637485699999981
9.329999999252436e-07
answered Apr 29, 2019 at 13:16
Sign up to request clarification or add additional context in comments.

2 Comments

sets are average case O(1) (hash collusions mean it could be worse). Also only worth building a set if you're doing to do multiple in tests (I'm sure you know this, but worth spelling it out)
@Chris_Rands we always talk about average case when talking time complexity (for example quicksort is always considered as O(nlogn) even though its worst case is O(n^2)), but I'll add it.
1

The difference in runtime is negligible between List, Tuple, and Set if there are only 3 elements.

Pep8 style guide doesn't say anything about this as far as I know, so you can use whichever you prefer.

Something the other answers missed, as far as readability goes, is you can declare a set directly like this:

if my_variable in {2, 'A', None}:
 print(True)
answered Apr 29, 2019 at 13:39

1 Comment

*Technically there is a difference with using Set over List or Tuple - objects are compared with hash rather than equality, defined in a class as __hash__ rather than __eq__
-1

A tuple is an immutable sequence, whereas a list is a mutable sequence, which means tuples can't be changes, but list can be.

If you don't want to modify the data structure you are checking in, use tuple, else use list, otherwise both behave the same.

my_variable = 'A'
if my_variable in [2, 'A', None]:
 print(True)
if my_variable in (2, 'A', None):
 print(True)

The output will be

True
True

Note that both list and tuple have O(n) time complexity to check, to get an average O(1) complexity, use a set.

if my_variable in set([2, 'A', None]):
 print(True)
#True
answered Apr 29, 2019 at 13:17

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.