Check if something is (not) in a list in Python
Asked Answered
A

4

465

I have a list of tuples in Python, and I have a conditional where I want to take the branch ONLY if the tuple is not in the list (if it is in the list, then I don't want to take the if branch)

if curr_x -1 > 0 and (curr_x-1 , curr_y) not in myList: 

    # Do Something

This is not really working for me though. What have I done wrong?

Anthropophagite answered 2/5, 2012 at 0:16 Comment(2)
Note that 3 -1 > 0 and (4-1 , 5) not in []True therefore the error is not one of operator precedence.Lombroso
Why not try myList.count((curr_x, curr_y)), if (curr_x, curr_y) is not in myList, the result will be 0Aldercy
G
741

The bug is probably somewhere else in your code, because it should work fine:

>>> 3 not in [2, 3, 4]
False
>>> 3 not in [4, 5, 6]
True

Or with tuples:

>>> (2, 3) not in [(2, 3), (5, 6), (9, 1)]
False
>>> (2, 3) not in [(2, 7), (7, 3), "hi"]
True
Gainey answered 2/5, 2012 at 0:18 Comment(1)
And this is documented here: docs.python.org/3/library/….Condottiere
M
56

How do I check if something is (not) in a list in Python?

The cheapest and most readable solution is using the in operator (or in your specific case, not in). As mentioned in the documentation,

The operators in and not in test for membership. x in s evaluates to True if x is a member of s, and False otherwise. x not in s returns the negation of x in s.

Additionally,

The operator not in is defined to have the inverse true value of in.

y not in x is logically the same as not y in x.

Here are a few examples:

'a' in [1, 2, 3]
# False

'c' in ['a', 'b', 'c']
# True

'a' not in [1, 2, 3]
# True

'c' not in ['a', 'b', 'c']
# False

This also works with tuples, since tuples are hashable (as a consequence of the fact that they are also immutable):

(1, 2) in [(3, 4), (1, 2)]
#  True

If the object on the RHS defines a __contains__() method, in will internally call it, as noted in the last paragraph of the Comparisons section of the docs.

... in and not in, are supported by types that are iterable or implement the __contains__() method. For example, you could (but shouldn't) do this:

[3, 2, 1].__contains__(1)
# True

in short-circuits, so if your element is at the start of the list, in evaluates faster:

lst = list(range(10001))
%timeit 1 in lst
%timeit 10000 in lst  # Expected to take longer time.

68.9 ns ± 0.613 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
178 µs ± 5.01 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

If you want to do more than just check whether an item is in a list, there are options:

  • list.index can be used to retrieve the index of an item. If that element does not exist, a ValueError is raised.
  • list.count can be used if you want to count the occurrences.

The XY Problem: Have you considered sets?

Ask yourself these questions:

  • do you need to check whether an item is in a list more than once?
  • Is this check done inside a loop, or a function called repeatedly?
  • Are the items you're storing on your list hashable? IOW, can you call hash on them?

If you answered "yes" to these questions, you should be using a set instead. An in membership test on lists is O(n) time complexity. This means that python has to do a linear scan of your list, visiting each element and comparing it against the search item. If you're doing this repeatedly, or if the lists are large, this operation will incur an overhead.

set objects, on the other hand, hash their values for constant time membership check. The check is also done using in:

1 in {1, 2, 3} 
# True

'a' not in {'a', 'b', 'c'}
# False

(1, 2) in {('a', 'c'), (1, 2)}
# True

If you're unfortunate enough that the element you're searching/not searching for is at the end of your list, python will have scanned the list upto the end. This is evident from the timings below:

l = list(range(100001))
s = set(l)

%timeit 100000 in l
%timeit 100000 in s

2.58 ms ± 58.9 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
101 ns ± 9.53 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

As a reminder, this is a suitable option as long as the elements you're storing and looking up are hashable. IOW, they would either have to be immutable types, or objects that implement __hash__.

Masonite answered 30/1, 2019 at 9:33 Comment(1)
Sets are not always an option (for example, when having a list of mutable items). For large collections: building the set for a lookup is O(n) time anyway and can be doubling your memory usage. If you don't already have a lookup around, it's not always the best choice to make/maintain one.Carmina
B
1

I know this is a very old question but in the OP's actual question of "What have I done wrong?", the problem seems to be in how they coded the following:

take the branch ONLY if the tuple is not in the list

As OP observes, this is logically equivalent to:

IF tuple in list THEN don't take the branch

It's, however, entirely silent on what should happen IF tuple not in list. In particular, it doesn't follow that

IF tuple not in list THEN take the branch

So OP's rule never mentions what to do IF tuple not in list. Apart from that, as the other answers have noted, not in is the correct syntax to check if an object is in a list (or any container really). For example:

# check if `my_tuple` is not in `my_list`
my_tuple not in my_list
Birkett answered 8/9, 2022 at 7:30 Comment(0)
C
0

One can also use the count method of list class: lets say we have a list:

x = [10,20,30,40,50]

To confirm if we have an element(i.e 10) in the list or Not and the frequency of its occurrence:

if x.count(10):
    print(x.count(10)) 
else:
    print(10," Not in the list")
Cambell answered 18/3, 2022 at 11:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.