Write Pythonic Code Like a Seasoned Developer Transcripts
Chapter: Generators and Collections
Lecture: Testing for containment

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Next, let's talk about testing for containment and various sequences If you want to look for an item in a set,
0:08 in a dictionary, in a list, those types of things, and you are new to Python you might look for some kind of find
0:14 or index of type of method on the type itself. But in Python, we have a special keyword to do this test, over here in PyCharm we have a list,
0:23 a set and a dictionary and the way we test for containment in them all the same, so if we'd run it, you can see we are just printing out these values
0:30 and if you look at the numbers, you probably recognize them as the Fibonacci sequence up to 34 anyway or five here where I had to write them out,
0:39 so what we are going to do is we are going to parse out a number gathered from the user, and then we are going to test whether this is in the set.
0:46 So, here we'll just do a few "if" tests and maybe we can do this as a tertiary sort of expression,
0:52 so we can say "print" like so, so we are going to say we'll print out something is in the set
0:58 and then we'll do out "if" test, we'll say "if n is in nums_list" and then maybe say a list here, keep the same order,
1:08 otherwise we'll say "not in list". All right, so the test here is "n in nums_list", all right,
1:15 so this actually goes through and it searches the elements in it and it does a comparison not on index but by value,
1:21 and then it'll tell you "yes or no it's here", then we could do the same thing as you'll see for the set and we could also do it for the dictionary.
1:31 All right, let's run it and figure it out, here it says enter a number to test for the small Fibonacci set or a sequence
1:37 and let's say well, say 21, 21 is in the list, 21 is in the set, but because I was lazy and didn't write them all out, 21 is not in the dictionary;
1:46 let's try again, how about 1, it should be in all of them yes, it's in. So, "if item in container", this even works for strings,
1:55 so if we had some text here like "Why did the multithreaded chicken cross the street?" do you know? Well it depends on when you ask it,
2:03 you'll always get a different answer, this time we are going to get "Other side to the get". So I could ask a question "word", so here we'll say
2:15 something, we'll do this not the tertiary way, we can say something like this, "if word in text: print such and such is in such and such",
2:30 there, so we could say ask user for a word, they type it in, we can do the same "in" test for a string, let's try,
2:37 first look for 7 which should not be there, now I'll look for chicken, chicken is in the "Why did the multithreaded chicken cross the street",
2:46 let's try it again, this time we'll enter 2 and here we'll put a cat. Right, so cat I don't believe appears in here. Cat is not in this string.
2:57 All right, so let's see that in a graphic, so here we are just going to work with dictionaries as you saw,
3:00 it's basically the same across the three types of containers we worked with. Here we could try to directly index into this dictionary and say
3:07 I want the thing with key 2 but as we saw in other examples, this could give us a KeyError, if it's not there,
3:12 so we might want to do this sort of check first style so we could say "if 2 is in the dictionary",
3:18 then we can safely access it because we know it's not going to KeyError.


Talk Python's Mastodon Michael Kennedy's Mastodon