🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

Membership Operators

AI & DATA SCIENCE // membership-operators

Membership operators (in, not in) test whether a value exists within a container — a string, list, tuple, set, or dict — returning True or False.

Syntax

x in container
x not in container

Deep Dive Course

`in` checks for membership by calling the container's __contains__ method, or falling back to iteration if that's not defined, and its performance depends heavily on the container type: checking a list or tuple is O(n), since it may scan every element, while checking a set or dict is average O(1) thanks to hashing. For a dict specifically, `in` checks the keys, not the values, unless you explicitly check the values view.

1Understanding Membership Operators

in checks for membership by calling the container's __contains__ method, or falling back to iteration if that's not defined, and its performance depends heavily on the container type: checking a list or tuple is O(n), since it may scan every element, while checking a set or dict is average O(1) thanks to hashing. For a dict specifically, in checks the keys, not the values, unless you explicitly check the values view.

💡

If you're doing repeated `in` checks against the same collection inside a loop, convert it to a set first — turning an O(n) scan per check into an O(1) lookup can make a big difference on large inputs.

editor.html
fruits = ["apple", "banana", "cherry"]
print("banana" in fruits)
print("grape" not in fruits)
localhost:3000

2Practical Example

Here is a real-world application of Membership Operators showing how it is used in production Python code.

editor.html
config = {"debug": True, "env": "prod"}
print("debug" in config)
print(True in config.values())
localhost:3000

3Best Practices

Follow these guidelines when working with Membership Operators:

1. Convert a list to a set before doing many repeated in checks against it, for O(1) average lookups instead of O(n)

2. Remember membership checks on a dict test its keys by default — check its values view explicitly to test values instead

3. Use not in directly instead of negating an in expression, for readability

⚠️

Tip: If you're doing repeated `in` checks against the same collection inside a loop, convert it to a set first — turning an O(n) scan per check into an O(1) lookup can make a big difference on large inputs.

editor.html
fruits = ["apple", "banana", "cherry"]
print("banana" in fruits)
print("grape" not in fruits)
localhost:3000

Examples

Example 01Basic Usage
fruits = ["apple", "banana", "cherry"]
print("banana" in fruits)
print("grape" not in fruits)
Example 02Advanced Example
config = {"debug": True, "env": "prod"}
print("debug" in config)
print(True in config.values())

Best Practices

  • Convert a list to a set before doing many repeated in checks against it, for O(1) average lookups instead of O(n)
  • Remember membership checks on a dict test its keys by default — check its values view explicitly to test values instead
  • Use not in directly instead of negating an in expression, for readability

Interview Question

Why is checking membership in a set generally faster than checking membership in a list for a large collection?

Hint: Think about how each container is stored internally.

A list is a plain sequence, so a membership check has to scan through elements one by one in the worst case, giving O(n) time. A set is backed by a hash table, so a membership check computes the value's hash and jumps almost directly to the bucket where it would be, giving average O(1) time regardless of how big the set is. For repeated membership checks against a large, unchanging collection, converting it to a set first is a common and effective optimization.

Exercises

MediumPractice using Membership Operators in a real scenario.
View Solution
fruits = ["apple", "banana", "cherry"]
print("banana" in fruits)
print("grape" not in fruits)

Frequently Asked Questions

Why is checking membership in a set generally faster than checking membership in a list for a large collection?

A list is a plain sequence, so a membership check has to scan through elements one by one in the worst case, giving O(n) time. A set is backed by a hash table, so a membership check computes the value's hash and jumps almost directly to the bucket where it would be, giving average O(1) time regardless of how big the set is. For repeated membership checks against a large, unchanging collection, converting it to a set first is a common and effective optimization.

Related Functions

setsdictionariesidentity-operators