Python Dictionaries
This is a sample chapter from the ebook Python for Data Science.
In this lesson, we will learn about a new data type in Python called Dictionaries. Dictionary is one of the most powerful data structures in Python. To understand what dictionaries are, let’s take an example.
Let’s say that an investor holds five stocks in his stock portfolio and he tallies the latest prices for each of these stocks. Let’s say the stocks are Google (GOOGL), Facebook (FB), Microsoft (MSFT), Apple (AAPL), and Amazon (AMZN). In Python, we can create a list to store the stock prices as shown below:
In [1]: prices = [849, 136, 64, 136, 848]
For the names or symbols of these stocks, we can create another list called symbols.
1In [2]: symbols = [“GOOGL”, “FB”, “MSFT”, “AAPL”, “AMZN”]
2Now, if we want to get the stock price of, say, the Microsoft stock, it’s a bit complicated here. Let’s see how we will do that with these lists.
First, we will use the list’s index method to get the stock index.
Ebook Sample
You're reading a sample chapter from Python for Data Science
Get the ebook to continue with the full material and included resources.
