More List Functions
- 02:05
How the use list functions in Python, including the 'in' operator to check if a value exists within a list, the 'len' function to determine the number of objects in a list, as well as Max and Min to find the largest and smallest values in a list.
Downloads
No associated resources to download.
Transcript
Sometimes it's useful to have a function to check to see if a value is already inside of a list. To check membership, you use the in operator and this results in a Boolean output of true or false. So if I wanted to check and see if Google, or in my tech portfolio, I could say print, and then I would type the object, which is the string for Google's ticker. Then I write in, which is that operator, that checks membership. And then I type the name of my list and that tells me that that is true Google is in that list.
Next, if I want to check the length of a list, which is how many objects are contained within a list, I use the Len function. So if I wanna just to see it again, I'm gonna print the actual list and then let's print the length. So LEN, Len of that list tech portfolio.
And here you can see the list, apple, Microsoft, Cisco, Google, and Nvidia. And the length of that list is 5, because there are five objects contained in the list. Now let's say that you wanted to look at the volatility of the stocks in this tech portfolio, and you had a list of the beta values for each of those tech stocks. You can use the max function and the min function to identify the largest and the smallest value in a list.
And it's really easy. You just type max and then in parentheses the name of the list.
And if I wanna also do the min, it's exactly the same.
There you go.