List Functions Workout
- 03:01
Practise using list functions in Python.
Downloads
No associated resources to download.
Glossary
list list function PythonTranscript
In your Jupyter Notebook file, complete the following steps. First, create and print a list called financials that contain strings for the tickers, JP Morgan, Bank of America, Wells Fargo, city, and American Express. Then step two change Citi to C, the correct ticker for Citi and print the new list. Third, remove AXP, the ticker for American Express and append PNC then print the new list. Fourth, check membership for BLK, the ticker for BlackRock, and print the result. And then finally, print the length of the list financials.
First, I'm going to create and print a list called financials, and it's gonna contain strings for the tickers corresponding to a few financial institutions.
And as a quick check, I'm gonna go ahead and print that out.
Looks like everything was entered correctly. I'm gonna create a new sell and move on. As a next step, we want to take Citi and change that value to the letter C. So I'm gonna write financials, and then I'm gonna put the index for Citi, which is index three.
And I'm gonna give that a new value. The letter C, let's print to check it. It looks good.
Now I want to remove American Express or AXP, and I'm going to add or append PNC. So let's call our list financials. And then we're gonna use, dot remove the remove function and in parentheses AXP to get rid of AXP. Then financials dot append PNC, and that's going to add PNC to the end of our list. Let's print it out to see how it looks.
So we have removed American Express and we've added PNC.
Now I'm going to check to see if BlackRock BLK is included in my list using the in function. And I'm simply gonna print the string BLK, followed by the in operator, which you can see turns green when I type it. And then the name of the list financials.
And that's going to give me a Boolean output, false. As we know, BLK is not in our list. And finally, we're going to print the length of our list using the len function, and that's gonna tell us that as we know, there are five objects in the list. Financials.