Python's "in" Operator: How to check for Membership

Learn to Code Today!

Learn 10x faster: coding, no-code and data skills. Join millions of users mastering new tech skills and accelerating their career with Enki.
Get started

Python's in operator is a straightforward tool that helps you determine membership within collections such as lists, tuples, and strings. Whether you’re checking if something is present or absent, understanding how this operator works is key for efficient coding. At Enki, we believe learning through exploration leads to better understanding.

Understanding the in Operator in Python

What is the in Operator?

The in operator is used to see if a particular element is present in an iterable like a list, tuple, string, set, or the keys of a dictionary. It returns True if the element exists and False otherwise. Note: In dictionaries, in checks only the keys and not the values.

How Does the in Operator Work?

Using in makes membership testing simple compared to using == with logical or. The in operator abstracts the iteration and comparison process, which makes your code cleaner and easier to follow. For lists, it checks elements sequentially. For sets and dictionaries, it uses hashing for faster lookups.

Using in with Different Data Types

Lists and Tuples

Membership tests in lists and tuples check each element one by one. Here's an example:

The code checks if 'banana' is amongst the items in the fruits list and finds it, returning True.

Strings

You can check if a substring is present within a string:

Instead of using complex string methods like find(), in handles substring checks efficiently. It checks if 'quick' is part of sentence, which it is, so we get a True.

Sets

Sets provide fast membership testing because of underlying hashing. Here's how it looks:

Checking if 'green' is in the colors set is quick and efficient, thanks to sets’ performance.

Dictionaries

With dictionaries, only keys are checked with in:

While in directly checks for 'Alice' among the keys, you’d use 'value' in student_scores.values() to check values.

The not in Operator

Negating Membership with not in

not in checks if an item is absent from a collection.

Above, the code returns True because 10 is not in the numbers list.

Use Cases

This operator can exclude elements, a feature useful in various filtering processes.

This example filters out 'simple' and 'test' from message, leaving only the desired words.

Advanced Usage and Considerations

Custom Objects with __contains__ Method

You can customize membership tests by defining __contains__() in your classes.

The class MyContainer takes a list of elements. By defining __contains__(), we've allowed in to work with our object.

Performance Considerations

in is efficient with dictionaries and sets due to quick lookups. However, it may become costly with large lists as it performs a linear search. For deep performance insights, you might refer to nkmk.me.

Wrapping Up

Mastering Python's in and not in operators helps write concise, readable, and efficient code. From simple checks to custom objects, these operators are versatile tools in Python. At Enki, we're here to guide you with more comprehensive guides and interactive practice, growing your coding skills one simple step at a time.

About Enki

  • Fully personalized online up-skilling
  • Unlimited AI coaching
  • Designed by Silicon Valley experts

More articles

Meet your AI-enabled coach

Professional athletes have a coach for every aspect of their performance. Why can’t you for your work? Enki’s AI-powered coaching on-demand - combined with state of the art, structured learning content - makes this a reality.
1
1:1 AI Coaching
How do I remove duplicate emails?
Convert the list to a set and back to a list. Sets automatically remove duplicates.
2
Personalized Exercises
3
Interactive practice

Unlock full access to all skills on Enki with a 7-day free trial

Get started