- Python Dictionaries
- Comparison Operators in Python
- Logical Operators in Python
- Conditional Statements in Python
- For Loop in Python
- While Loop in Python
- How to loop over python dictionaries and Numpy arrays
- What is NumPy in Python
- ndarray - Methods and Data Type
- NumPy - Methods to Create Arrays
- Python NumPy - Numerical Operations on Arrays
- Python NumPy - Indexing and Slicing Arrays

# Python Dictionaries

```
In [1]: prices = [849, 136, 64, 136, 848]
```

For the names or symbols of these stocks, we can create another list called symbols.

```
In [2]: symbols = [“GOOGL”, “FB”, “MSFT”, “AAPL”, “AMZN”]
```

Now, 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.

```
In [3]: msft_index = symbols.index(“MSFT”)
In [4]: msft_index
Out [4]: 2
```

Now, we can use this index to get the price of the Microsoft stock from the 'prices' list.

```
In [5]: prices[msft_index]
Out[5]: 64
```

Clearly, this is possible but cumbersome. We could have got the results much more efficiently if our data was in a dictionary. The Dictionary is a powerful data structure that has a ‘key’ and a ‘value’. Each key is unique in the dictionary, and it has an associated value. The associated value however, does not need to be unique. Think of it as a telephone directory, where there is a name and a corresponding telephone number for each name.

We can present the above stock price data in the form of a dictionary as follows. Let’s call our dictionary ‘portfolio’.

```
In [1]: portfolio = {“GOOGL”:849, “FB”:136, “MSFT”:64, “AAPL”:136, “AMZN”:848}
```

Notice the curly brackets for the entire dictionary and the use of colon to separate the keys and values. With this dictionary, we can get the stock price of any stock with just one command:

```
In [2]: portfolio[“MSFT”]
Out [2]: 64
```

Similarly, we can access the price of the Apple stock by passing the key in square brackets as follows:

```
In [3]: portfolio[“AAPL”]
Out [3]: 136
```

This method is not just easy but also very fast even if we are dealing with a large dataset.

As noted earlier, the keys in a list must be unique. If you try to add another key:value pair with a key that already exists, the latest key:value pair will remain in the dictionary.

### Adding Data to a Dictionary

Let’s say you added one more stock (Twitter) to your portfolio and want to add its price to the dictionary. You can do so by writing the key ‘TWTR’ in square brackets and assign the stock price to it.

```
In [1]: portfolio[“TWTR”] = 16
```

Now if you check the dictionary, you will find the new key:value pair in it.

```
In [2]: portfolio
Out [2]: {'GOOGL': 849, 'FB': 136, 'MSFT': 64, 'AAPL': 136, 'AMZN': 848, 'TWTR': 16}
```

You’ve successfully added a new key:value pair to the dictionary.

### Removing Data from a Dictionary

Now suppose you decided to sell the Microsoft stock and want to remove its stock price from the dictionary. You can do so using the del command.

```
In [3]: del(portfolio[“MSFT”])
In [4]: portfolio
Out [4]: {'GOOGL': 849, 'FB': 136, 'AAPL': 136, 'AMZN': 848, 'TWTR': 16}
```

You’ve successfully removed the Microsoft stock from the dictionary.

### Advanced Dictionaries

As we know, lists can hold any kind of data. The same is true for dictionaries. In dictionaries, the value can again be other dictionaries containing more information. In our portfolio example, we might want to store the price of each stock along with the quantity of each stock held in the portfolio. Our dictionary with this data will look as follows:

```
In [1]: portfolio = {'GOOGL': {“price”: 849, “qty”: 5}, 'FB': {“price”: 136, “qty”: 4}, 'AAPL': {“price”: 136, “qty”: 6}, 'AMZN': {“price”: 848, “qty”: 3}, 'TWTR': {“price”: 16, “qty”: 10}}
```

We can now access the data using the square bracket notation. For example, the quantity of Apple stock will be access as follows:

```
In [2]: portfolio[‘AAPL’][‘qty’]
Out [2]: 6
```

This way you can retrieve any information from nested dictionaries.

Both lists and dictionaries are very useful. In terms of deciding when to use what, there is a simple rule you can follow. If you have a collection of values where the order of values is important and you want to be easily subset the data, lists are a good choice. However, you should use a dictionary when you have a set of unique keys that map to values and you want it to be easy and fast to lookup these values.