in operator in Python (for list, string, dictionary, etc.) – Programming Code Examples
Python

in operator in Python (for list, string, dictionary, etc.)8 min read

In Python, the operators in and not in test membership in lists, tuples, dictionaries, and so on.

This article describes the following contents.

  • How to use the in operator
    • Basic usage
    • With if statement
    • in for the dictionary (dict)
    • in for the string (str)
  • not in (negation of in)
  • in for multiple elements
    • Use andor
    • Use sets
  • Time complexity of in
    • Slow for the list: O(n)
    • Fast for the set: O(1)
    • For the dictionary
  • in in for statements and list comprehensions

How to use the in operator in Python

Basic usage




x in y returns True if x is included in y, and False if it is not.

Not only list, but also tuplesetrange, and other iterable objects can be operated.

The dictionary (dict) and the string (str) are described later.

With if statement

in returns a bool value (TrueFalse) and can be used directly in if statement.

Note that lists, tuples, strings, etc. are evaluated as False if they are empty, and as True if they are not. If you want to check whether an object is empty or not, you can use the object as it is.

“in” for the dictionary (dict)

The in operation for the dictionary (dict) tests on the key.

Use values()items() if you want to test on values or key-value pairs.

“in” for the string (str)

The in operation for the string (str) tests the existence of a substring.

not in (negation of “in”)

x not in y returns the negation of x in y

The same result is returned by adding not to the entire in operation.

However, if you add not to the entire in operation, it will be interpreted in two ways, as shown below, so it is recommended to use the more explicit not in.

Since in has a higher precedence than not, it is treated as the former if there are no parentheses.

The latter case is recognized as follows.

“in” for multiple elements

If you want to check if multiple elements are included, using a list of those elements as follows will not work. It will be tested whether the list itself is included or not.

Use andor or sets.

Use and, or

Combine multiple in operations using and and or. It will be tested whether both or either are included.

Since in and not in have higher precedence than and and or, parentheses are not necessary. Of course, if it is difficult to read, you can enclose it in parentheses as in the last example.

Use sets

If you have a lot of elements you want to check, it is easier to use the set than andor.

For example, whether list A contains all the elements of list B is equivalent to whether list B is a subset of list A.

Whether list A does not contain the elements of list B is equivalent to whether list A and list B are relatively prime.

If list A and list B are not relatively prime, it means that list A contains at least one element of list B.

Time complexity of “in”

The execution speed of the in operator depends on the type of the target object.

The measurement results of the execution time of in for lists, sets, and dictionaries are shown below.

Note that the code below uses the Jupyter Notebook magic command %%timeit and does not work when run as a Python script.

Take a list of 10 elements and 10000 elements as an example.

The sample code below is executed in CPython 3.7.4, and of course, the results may vary depending on the environment.

Slow for the list: O(n)

The average time complexity of the in operator for lists is O(n). It becomes slower when there are many elements.

The execution time varies greatly depending on the position of the value to look for. It takes the longest time when its value is at the end or when it does not exist.

Fast for the set: O(1)

The average time complexity of the in operator for sets is O(1). It does not depend on the number of elements.

The execution time does not change depending on the value to look for.

If you want to repeat in operation for a list with many elements, it is faster to convert it to a set in advance.

Note that it takes time to convert a list to a set, so it may be faster to keep it as a list if the number of in operations is small.

For the dictionary

Take the following dictionary as an example.

As mentioned above, the in operation for the dictionary tests on keys.

The key of the dictionary is a unique value as well as the set, and the execution time is about the same as for sets.

On the other hand, dictionary values are allowed to be duplicated like a list. The execution time of in for values() is about the same as for lists.

Key-value pairs are unique. The execution time of in for items() is about set + α.

“in” in for statements and list comprehensions

The word in is also used in for statements and list comprehensions.

Note that the in operator may be used as a conditional expression in list comprehensions, which is confusing.

The first in is in for the list comprehensions, and the second in is the in operator.

Source:

https://note.nkmk.me/

Leave a Comment