Loading video player…

Using Membership Checks With Common Data Types

00:00 In this lesson, you’ll try out using the membership operators with a couple of common data structures. So I’m here in the Python REPL, and let’s start off by checking whether a number is in a tuple.

00:10 So I could say 3 in, and then I’ll make a tuple here. I contain 1, 2, and 3 so we already know the answer. And if I check is 3 in (1, 2, 3), Python replies with that’s true, it’s in there.

00:24 Let’s try another one. Let’s try for a string. So, I want to check whether the letter o is in the word rose. So I type o in quotes in, and then in quotes, rose.

00:37 And Python again replies with True. Now I could do the same, and if I say not in rose, Python replies with False.

00:48 So as you can see, in and not in just do the inverse of each other. In this case, if Python finds the character o inside of the string rose, then it replies with True.

00:58 And if you use not in, then it replies with False. Before we move on to different data structures, I want to point out something to be aware of when you’re working with strings and membership operators, because the empty string in Python is a little special.

01:12 So if I would go ahead and type this two quotes without any other content in there, and then I say in, and again, let’s stick with rose.

01:21 What do you think this is going to return if I press Enter?

01:25 Well, it returns True, and it always will. In Python, the empty string is always part of any string. This is something to be aware of. On the other hand, if you add a whitespace character in between those quotes and then perform the check, it’ll be False because there is no space in rose.

01:42 So just a little gotcha to be aware of—the empty substring is always part of any string in Python.

01:48 So much about strings. Let’s move on to lists, which is maybe one of the most common things that you’ll use this membership checks on. So I could check is rose not in, and then I’ll open up a list with two square brackets and I’m going to type a couple of words in there.

02:05 For example, sweet, red, and thorny,

02:10 and press Enter here. You’ll see that Python returns True, again, because the word rose is not inside of the list that contains a couple of words, but not the word rose.

02:21 And then let’s try one more with a dictionary too. So I’m going to make a dictionary first that we can reuse rose_dict = opening and closing the curly braces, and then in here I put a couple of key-value pairs.

02:36 I’m going to say name: "rose",

02:41 both strings, and then also let’s add smell: "sweet" also strings. This is our rose_dict.

02:52 And now I can check for membership directly. I can say is name in rose_dict and it’ll return True because Python iterates over the keys of dictionary by default.

03:03 So you can see that the name key is in there and the smell key would also be in there. So that would also return True, but if I were to say is

03:12 "rose" in rose_dict just like that, then that’ll be False because by default Python just checks the keys. Now if you do want to check the values, you’ll have to add .values() at the end.

03:26 Oops, I forgot to add an s here. And just typed value instead of values. So Python gave me an Attribute Error, so let’s go ahead and fix that.

03:33 rose_dict.values(). This needs to be plural. This is going to allow me to access the values of the dictionary, not the keys. And now "rose" in rose_dict.values() returns True.

03:46 Alright, so as you can see, all of these work in the same way, right? You use the value that you want to check, then you use the membership operator either in or not in, and then specify the object that you want to check over.

03:59 Like I said, often this is some sort of a collection, like a list or a tuple, but it can also be a mapping, like a dictionary, for example, or a set. So that should give you some sort of an intuitive understanding of what the membership operators do and also how you can actually use them on a lot of different objects.

04:18 Now enough of this really small example, let’s go ahead and build a slightly bigger script that uses the membership operators in practice.

Become a Member to join the conversation.