Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Working With Membership Operators

Resource mentioned in this lesson: Python’s “in” and “not in” Operators: Check for Membership

00:00 How can you test whether a value can be found in a container? Some obvious methods probably come to mind, but in Python we have operators dedicated to performing that kind of check: membership operators.

00:11 They test whether a value is present in a collection. They take a value and a container type as operands, they always return Boolean values, and they work as binary operators.

00:23 The in operator is used in expressions like value in collection and returns true if value is present in collection, false otherwise. The not in operator uses the syntax value not in collection and returns true if value is not found in collection, false otherwise. And again, you can use the syntax, not value in collection, but it’s a little awkward and harder to read, so value not in collection is the preferred version.

00:53 Time to open the REPL and write some code. Usage of in is pretty straightforward. Create a list called numbers.

01:02 numbers = [2, 3, 5, 9] and try 5 in numbers.

01:09 This returns True. How about 5 not in numbers.

01:14 This returns False because 5 is in numbers. And with a value that you know isn’t in numbers like 8.

01:22 8 in numbers returns False. 8 not in numbers returns True. Again, pretty intuitive and works as you’d expect for other containers like tuples and sets as well.

01:34 But things are a bit more interesting with dictionaries. Create a dictionary called likes. Its keys and values can all be strings. likes = the dictionary with the key-value pairs {'color': and 'blue', 'fruit': 'apple'

01:51 'pet': 'cat'}. So is 'fruit' in likes? fruit in likes returns True. fruit is indeed one of the keys.

02:01 Is hobby in likes? False, it isn’t present anywhere in the dict. But is blue in likes? False. This might be surprising because blue is one of the values, but the in operator only checks membership among dictionary keys.

02:18 So if you do want to check among the values, you have to use the .values method of dictionaries. This returns a collection of the dictionaries values.

02:27 blue in likes.values() returns True, while fruit in likes.values() returns False because fruit is key and not a value.

02:40 Another interesting use of in is with strings. Create the variable greeting. greeting equals the string "Hi, welcome to Real Python!"

02:50 Is the string "welcome" in greeting? welcome in greeting returns True, so yes. And of course, "welcome" not in greeting returns False.

03:02 Accordingly, is "Hello" in greeting? "Hello" in greeting. The result is False, so the opposite. "Hello" not in greeting returns True. With strings, it’s important to note that this is case sensitive and searches for exact substring matches.

03:23 So it is a little limited compared to more advanced approaches like regular expressions. Just something to keep in mind. If you’d like to see more examples of these operators applied to all sorts of data types, check out “Python’s in and not in Operators: Check for Membership”.

03:40 And now that you’ve seen all the operators, I have to show you, you might wonder how they’re resolved when they’re mixed in larger expressions. Find out in the next lesson.

Become a Member to join the conversation.