Loading video player…

Understanding the Basics of "in" and "not in"

00:00 In this lesson, I’ll quickly walk you over the basics of in and not in. Then in the next lesson, we’re going to hop into the REPL to do some coding.

00:08 But let’s start off by considering the basics of in and not in. So both of them are Boolean operators, so they return True or False depending on the result of the membership check.

00:18 The general form is that if you use the in operator, then you say value in container. And if you use the not in operator, it works the same, type it as value not in container.

00:29 And like I already said previously, you’ll often use them in conditional checks.

00:33 I also mentioned that they work both with containers and iterators. You’ll look into this a bit more later on, but most commonly, you’ll be using it with a data structure, like a list or a tuple, or maybe a string.

00:45 But you can also use them with generators, for example, which are iterators. And we’ll look at this a little bit later in the course.

00:53 Which brings us to this slide that lists out all the different objects that you can use membership operators on. There’s a lot of them, I’m not even going to read them out, but at the beginning you’ll see a couple of very common ones, and these are the ones we’re going to look into because first of all, there’s too many to tackle in one course, and also the syntax is always the same.

01:11 That’s the cool thing about these types of constructs in Python. To a certain degree, Python doesn’t care what type of data structure you’re checking membership on.

01:20 You can use the same syntax across lists, strings, and even generators, and Python will handle the necessary differences under the hood. So it’s good to know that you can use it on a lot of different objects.

01:31 Also, it does work on many objects, but it doesn’t work the same on all objects. One aspect of using this interface of just value in container across all of those objects is that Python under the hood actually does different things depending on what objects you are checking the membership on.

01:49 And I’ll just talk about a couple of them. Most notably, sequences such as lists and tuples, they use iteration to check for membership, strings, which use substring checking, and mapping, such as dictionaries or sets, which use hashing.

02:03 Now you may think, okay, so Python uses different approaches, but how is that relevant? And truth is it may not be relevant for you, but if you are running it on large data structures, it may be relevant because the performance is very different between these different approaches.

02:18 Iteration is quite slow. Substring checking is a little bit faster because there are some C-level optimizations that Python does, and hashing on dictionaries and sets is very fast.

02:28 So if you do need to do membership testing on very large data structures, then it’s best to use a dictionary or a set.

02:36 I’m not going to go into performance more, but if you’re interested in that, then go ahead and check out the associated tutorial. You can click on the link if you have the slides handy, and the written tutorial goes a little bit into comparing the performance between these different approaches.

02:51 Alright, so that’s enough theoretical intro. In the next lesson, you’ll try out membership checks with a couple of common data structures.

Become a Member to join the conversation.