Skip to main content
Taught by Tech Leads

Master pipelines, cloud & AI to become an operational Data Engineer.

DataScientist.fr
In the vast world of Python programming, mastering membership tests is crucial for writing both efficient and readable code. The operators 'in' and 'not in' play a key role in checking for the presence of elements in sequences, offering a simple and intuitive syntax. This article explores in depth the use of these operators, from concrete examples to their integration into custom classes, along with tips for optimizing your membership tests. Discover how these tools can transform your coding approach and simplify your development processes.

Getting started with membership tests

The membership operators in Python, namely in and not in, are powerful tools for checking if an element is present in a data structure, such as a list, tuple, or dictionary. Let's start by exploring how these operators work and how to use them effectively.

Basic usage of in and not in

The in operator is used to check if an element exists in a sequence. For example, let's say we have a list of fruits:
python
To check if 'apple' is in the list, we use:
python
On the other hand, to check if an element is not present, we use not in:
python
These operators are intuitive and facilitate the process of checking for the presence of an element without having to write explicit loops.

Membership operators with strings

The use of membership operators is not limited to lists; they also apply to strings. For instance:
python
Here, in checks if the substring 'mour' is present in the phrase string.

Advanced use cases

Membership operators can also be used with dictionaries, but with a slight difference. When used with a dictionary, in and not in only check for the presence of keys and not values:
python

Performance of membership tests

It is important to note that the performance of membership tests can vary depending on the data structure. For example, checking for membership in a set (set) or a dictionary is generally faster than in a list, due to how these data structures are implemented in Python.
python
In summary, the use of the in and not in operators in Python is not only intuitive but also essential for writing efficient and readable code. These operators allow for quick checks and are fundamental elements for anyone working with data collections in Python.

Using the 'in' operator

The in operator is a versatile tool that can be used in various contexts in Python. In this section, we will explore some of the most common and useful applications of this operator.

Checking in lists

Checking for the presence of an element in a list is probably the most common use of the in operator. Here’s a simple example:
python
This concise syntax allows for quick and efficient verification of whether an element is present, which is particularly useful for data validation or searching for specific items in large lists.

Using with iterables

The in operator works not only with lists but also with any iterable, including tuples and sets:
python
This flexibility allows in to be used in a variety of contexts, making the code more adaptable and easier to maintain.

Application in dictionaries

As mentioned earlier, in is used to check for the presence of keys in a dictionary. However, it can also be used in combination with methods like .values() and .items() to check for the presence of values or key-value pairs:
python

Loops and conditions

The in operator is often used in for loops to iterate over elements:
python
This simplifies the process of iterating over collections and makes the code more readable. Additionally, in is commonly used in conditions to make decisions based on the presence of elements.
The in operator is essential for writing concise and efficient Python code. Whether for membership checks, iterating over collections, or interacting with complex data structures, in is an indispensable tool for any Python developer.

Using the 'not in' operator

The not in operator is the logical complement of in and is used to check for the absence of an element in a sequence or data structure. This check is often necessary in many programming scenarios to ensure that an element is not present in a collection before proceeding with a specific operation.

Avoiding duplicates

One common application of not in is to prevent the insertion of duplicates in a list:
python
In this example, not in ensures that new_number is added to numbers only if it is not already present.

Input validation

not in is also used in input validation to ensure that incorrect or undesirable data is not processed:
python
This type of check is crucial in applications where access must be restricted to specific users.

Error handling

In some cases, not in can help avoid errors by ensuring that certain conditions are met before executing an operation:
python
This allows for proactive error state management and informs users of the absence of an item.

Using in loops

not in can also be used in loops to filter elements:
python
In this example, not in is used to skip certain elements during iteration, which is handy for applying filters or specific conditions.
In summary, the not in operator is a powerful tool for managing the absence of elements in Python collections, allowing for error prevention, data integrity assurance, and resource access control.

Using 'in' and 'not in' with different Python types

The in and not in operators prove to be extremely versatile in Python, as they can be applied to various data types. Let's examine how these operators interact with different types of Python structures.

Lists

Lists are probably the most commonly used type with in and not in. These operators allow checking for the presence of an element:
python
The flexibility of these operators simplifies checks and manipulations of lists.

Strings

With strings, in and not in check for the presence of substrings:
python
This is particularly useful for search and filtering operations in texts.

Tuples

Tuples, while immutable, also support the use of in and not in:
python
Using these operators with tuples is ideal for checking the presence of elements in immutable collections.

Dictionaries

In dictionaries, in and not in check for the presence of keys by default, but can also be used with .values() and .items() to check for values or pairs:
python
This allows for great flexibility when working with associative data.

Sets

Sets, designed for membership and uniqueness operations, work particularly well with in and not in due to their efficiency:
python
Sets are perfect for quick presence or absence checks.
In conclusion, the ability to use in and not in with various data types in Python offers great flexibility and simplifies many programming tasks, from text processing to managing complex collections.

Concrete examples of using the 'in' and 'not in' operators

To illustrate the practical application of the in and not in operators, here are some concrete examples that show how these tools can be integrated into real programming scenarios.

Data filtering

Imagine you have a list of products and you want to extract only those that are not out of stock:
python
This example shows how not in can be used to quickly filter out undesirable items.

Unique ID validation

When creating unique identifiers, in can be used to ensure that a new identifier is not already in use:
python
This method guarantees the uniqueness of identifiers in your system.

Text analysis

Text analysis is another area where in is often used to search for specific words or phrases:
python
This allows for quick identification of relevant keywords in a given text.

Access management

In user management systems, not in can be used to check if a user does not have access to a particular resource:
python
This check ensures that only authorized users can access certain features or data.
These examples demonstrate the versatility of the in and not in operators in Python, enabling developers to simplify their code and make verification operations more efficient and readable.

Replacing chained 'or' operators

In certain programming scenarios, it may be tempting to use or operators to check if a value matches multiple options. However, using in and not in can simplify and make the code more readable and efficient.

Classic usage of or

Let's consider an example where you want to check if a variable fruit is one of the fruits you are looking for:
python
Although this approach works, it quickly becomes verbose and difficult to maintain as the list of fruits grows.

Simplification with in

By replacing or operators with in, the code becomes more concise:
python
This method uses a list to store possible values and checks for membership in a single operation, thus improving code readability and maintainability.

Advantages of using in

Using in offers several advantages:
  • Readability: The code is easier to understand, especially when there are many options.
  • Scalability: Adding or removing options in the list is simple and does not require modifying the conditional logic.
  • Performance: With data structures like sets, membership checking is optimized, which can provide performance gains.

Complex case with not in

Similarly, for absence checks, not in can be used to simplify complex expressions with or:
python
In this case, not in is used to ensure that fruit does not belong to a list of specific items, in a concise and efficient manner.
By replacing chained or operators with in and not in, the code becomes not only clearer but also easier to manage and adapt to future changes.

Writing efficient membership tests

Writing efficient membership tests is crucial for optimizing the performance and readability of Python code. By using the in and not in operators wisely, it is possible to perform quick and accurate checks. Here are some best practices to achieve this.

Choosing the right data structure

The performance of membership tests heavily depends on the chosen data structure. Sets (set) and dictionaries are generally more performant than lists and tuples for membership checks:
python
Sets use hash tables, which allow for constant time lookups, unlike lists which require sequential traversal.

Prefer lists for small collections

For small collections, the performance impact is often negligible, and the simplicity of using lists may be preferred:
python

Using list comprehensions

List comprehensions allow for efficiently combining membership tests with the creation of new lists:
python
This method is both concise and effective, especially for filtering elements.

Optimization with nested conditions

For complex checks, combining in and not in with other conditions can optimize control flow:
python
By following these practices, you can write membership tests that are not only efficient but also easy to read and maintain. Optimizing these tests is essential for improving performance and ensuring the robustness of your code.

Using 'operator.contains()' for membership tests

Using operator.contains() in Python provides a powerful and sometimes more explicit alternative for performing membership tests. This function is part of the operator module, which provides functions equivalent to Python's intrinsic operators.

Understanding operator.contains()

The function operator.contains(container, item) is used to check if an item is present in a container. It returns True if the item is found; otherwise, it returns False. This function is equivalent to using item in container, but it can be useful in certain contexts, especially when using functions like map() or filter().

Using in functions

When working with functions like map() or filter(), operator.contains() can be used to make the code more readable and avoid lambda function definitions:
python
This code shows how operator.contains() can be used to enhance clarity when integrated into functional programming constructs.

Comparison with in

Although operator.contains() is not necessary in simple cases, it offers a more explicit semantics that can be beneficial in complex contexts or for developers who prefer a functional approach:
python
This approach is particularly useful in environments where functions need to be passed as arguments or when aiming to make the code clearer for readers.

Conclusion on using operator.contains()

While the in operator is more natural and often sufficient, operator.contains() offers a valuable alternative in situations where a functional approach is preferred, thus enhancing code modularity and readability. This demonstrates Python's flexibility to cater to different programming styles and specific needs.

Support for membership tests in user-defined classes

To enable the use of in and not in operators in your own Python classes, you need to implement the special method __contains__. This method is called by Python to determine if an element belongs to an instance of your class. Here’s how you can proceed.

Implementing __contains__

To add support for membership tests in a user-defined class, start by defining the __contains__ method. Let's imagine we have a Library class that contains a collection of books:
python
In this example, the __contains__ method checks if a book is present in the library's collection of books.

Using in code

With the __contains__ method implemented, you can use the in and not in operators naturally with instances of your class:
python
This approach makes the code more readable and intuitive, allowing the use of familiar syntax with your own data types.

Advantages of using __contains__

Implementing __contains__ has several advantages:
  • Readability: It allows for idiomatic Python syntax, making the code easier to understand.
  • Encapsulation: It keeps the membership logic encapsulated within the class, facilitating code maintenance and evolution.
  • Flexibility: You can customize the membership logic to meet the specific needs of your application.
By implementing __contains__, your classes can easily integrate into the Python ecosystem, using concepts and operators that are already familiar to developers. This not only enhances the coding experience but also improves the robustness and reusability of your code.

Conclusion

The membership operators in and not in are essential tools in Python, providing a simple and intuitive way to check for the presence or absence of elements in sequences and other data structures. Their effective use can simplify code, enhance readability, and optimize performance.

Key takeaways

  • Versatility: These operators apply to various data types, from lists to sets, dictionaries, and strings. Their ability to adapt to different contexts makes them indispensable for any Python developer.
  • Optimization: Using in and not in with the right data structures, such as sets for large collections, can significantly improve code efficiency. Choosing the right structure based on data size and complexity is crucial for maintaining optimal performance.
  • Simplicity: The ability to replace chains of or operators with in enhances readability and reduces code complexity. This simplification is particularly useful when handling many conditions or checks.

Integration into custom classes

By implementing the special method __contains__, developers can extend these functionalities to user-defined classes, making the code more consistent and integrated with the Python language. This integration allows for a more intuitive and idiomatic use of custom classes, facilitating their adoption and use in larger projects.

Functional approach

Using operator.contains() provides a functional alternative for membership tests, allowing greater flexibility in contexts where functions need to be passed as arguments or used in functional expressions.
By adopting these practices, developers can write efficient membership tests that are both performant and easy to maintain. Integrating these concepts into everyday programming not only improves code quality but also enhances the overall development experience.
Whether you are a beginner or an experienced developer, understanding and mastering the use of in and not in operators is essential for fully leveraging the power and flexibility of the Python language.

Want to go further?

This topic is part of our Become a Data Analyst course. Browse the full programme, or get it by email.

Share with

Photo de Romain DE LA SOUCHÈRE

Romain DE LA SOUCHÈRE

Tech Lead, CTO AXI Technologies

Expert Data Engineering et Cloud, Romain affiche plus de 11 ans d'expérience, dont plusieurs années comme Lead Developer sur des solutions Smart Building haute performance. Il y a conçu et mis en production des moteurs de traitement capables d'absorber des centaines de milliers de données de capteurs par minute, ainsi que des bases clusterisées gérant plus de 10 millions de données dynamiques. Certifié Microsoft Azure DevOps Engineer Expert, il maîtrise aussi bien le développement back-end (Python, C#) que le DevOps (Docker, Kubernetes, Terraform) et les agents LLM. Formateur en Python, cloud, DevOps et IA générative appliquée, il forme avec une obsession : Amener chaque apprenant à concevoir et déployer des architectures réellement scalables en production.

» Learn More

Associated trainings

All our trainings
Image de la formation Become a Data Analyst
Become a Data Analyst
6 months
Intermediate
Guarantee

Associated articles

See all our articles