Write Pythonic Code Like a Seasoned Developer Transcripts
Chapter: Generators and Collections
Lecture: Testing for containment
0:01 Next, let's talk about testing for containment and various sequences
0:05 If you want to look for an item in a set,
0:07 in a dictionary, in a list, those types of things,
0:10 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.
0:17 But in Python, we have a special keyword to do this test,
0:20 over here in PyCharm we have a list,
0:22 a set and a dictionary and the way we test
0:24 for containment in them all the same,
0:26 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
0:33 as the Fibonacci sequence up to 34 anyway
0:36 or five here where I had to write them out,
0:38 so what we are going to do is we are going to parse out
0:40 a number gathered from the user,
0:42 and then we are going to test whether this is in the set.
0:45 So, here we'll just do a few if tests and maybe we can do this
0:48 as a tertiary sort of expression,
0:51 so we can say print like so, so we are going to say
0:54 we'll print out something is in the set
0:57 and then we'll do out if test we'll say if in is in nums list
1:04 and then maybe say a list here, keep the same order,
1:07 otherwise we'll say not in list.
1:10 All right, so the test here is n in nums list, all right,
1:14 so this actually goes through and it searches the elements in it
1:17 and it does a comparison not on index but by value,
1:20 and then it'll tell you yes or no it's here,
1:22 then we could do the same thing as you'll see for the set
1:25 and we could also do it for the dictionary.
1:30 All right, let's run it and figure it out,
1:32 here it says enter a number to test for the small Fibonacci set or a sequence
1:36 and let's say well, say 21, 21 is in the list, 21 is in the set,
1:41 but because I was lazy and didn't write them all out 21 is not in the dictionary;
1:45 let's try again, how about 1, it should be in all of them yes, it's in.
1:48 So, if item in container, this even works for strings,
1:54 so if we had some text here like "Why did the multithreaded chicken cross the street?"
1:59 do you know? Well it depends on when you ask it,
2:03 you'll always get a different answer,
2:04 this time we are going to get "Other side to the get".
2:08 So I could ask a question word, so here we'll say
2:14 something we'll do this not the tertiary way, we can say something like this,
2:17 if word in text print such and such is in such and such, there,
2:29 so we could say ask user for a word, they type it in,
2:32 we can do the same in test for a string, let's try,
2:36 first look for 7 which should not be there, now I'll look for chicken,
2:42 chicken is in the "Why did the multithreaded chicken cross the street",
2:45 let's try it again, this time we'll enter 2 and here we'll put a cat.
2:51 Right, so cat I don't believe appears in here.
2:54 Cat is not in this string.
2:56 All right, so let's see that in a graphic,
2:58 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.
3:02 Here we could try to directly index into this dictionaries and say
3:06 I want the thing with key 2 but as we saw in other examples,
3:09 this could give us a key error, if it's not there,
3:11 so we might want to do this sort of check first style
3:14 so we could say if 2 is in the dictionary,
3:17 then we can safely access it because we know it's not going to key error.