- 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

# How to loop over python dictionaries and Numpy arrays

In the previous lessons, we learned about how to loop over lists. In this lesson, we will learn about how to loop over python dictionaries and Numpy arrays.

### Loop over dictionaries

To loop over a dictionary elements, we use the `items()`

method provided with a dictionary object. Let's say we have the following dictionary containing the a list of stocks along with their prices.

```
portfolio = {"GOOGL":849, "FB":136, "MSFT":64, "AAPL":136, "AMZN":848}
```

As you can see, the dictionary contains key-value pairs of stock symbols and their prices. We can iterate over it and print each stock's information on a different line as follows (using for loop).

```
for key, value in portfolio.items() :
print("The stock " + key + " has a price of " + str(value))
```

Results:

```
The stock GOOGL has a price of 849
The stock FB has a price of 136
The stock MSFT has a price of 64
The stock AAPL has a price of 136
The stock AMZN has a price of 848
```

### Loop Over Numpy Array (1d)

If we have a one-dimensional Numpy array, looping over it is easy. Consider the following Numpy array:

```
prices = np.array([5,8,12.7,89.6,12.9,5.4])
```

We can iterate over the elements as follows:

```
import numpy as np
prices = np.array([5,8,12.7,89.6,12.9,5.4])
for price in prices :
print ("The price is: " + str(price))
```

Results:

```
The price is: 5.0
The price is: 8.0
The price is: 12.7
The price is: 89.6
The price is: 12.9
The price is: 5.4
```

### Loop over Numpy Arrays (Multi-dimensional)

For multidimensional arrays, iteration proceeds over the first axis such that each loop returns a subsection of the array. Consider the following array:

```
a = np.array([[ 1., 2., 3.],
[ 4., 5., 6.]])
```

The result will be:

```
[ 1. 2. 3.]
[ 4. 5. 6.]
```

However, if you wanted to iterate over each separate element of the multi-dimensional array, you can do so using the `nditer()`

method as shown below:

This will print each element separately as shown below:

```
1.0
2.0
3.0
4.0
5.0
6.0
```

Let's take another example. Consider an array which contains original loan amounts and the balance principal outstanding on 5 different stocks. All figures are in '000.

```
loan_summary = np.array([[25,10],[30,15],[30,18],[60,30],[40,7]])
```

Suppose we want to print the results in this manner - "Y principal outstanding on a loan of X". To do so, we can use multiple assignments as shown below:

```
for (x, y) in loan_summary:
print (str(y) + " principal outstanding on a loan of " + str(x))
```

Results:

```
10 principal outstanding on a loan of 25
15 principal outstanding on a loan of 30
18 principal outstanding on a loan of 30
30 principal outstanding on a loan of 60
7 principal outstanding on a loan of 40
```

### Exercise

Consider our loan example, but this time as a list with each item of the list containing three pieces of information: the borrower's name, the original loan amount, and the principal outstanding.

```
# Loan details
loans = [["John", 25,10],
["Ken", 30,15],
["Mat", 30,18],
["Ron", 60,30],
["David", 40,7]]
```

You need to build a for loop from scratch and print each loan's information in a separate line in this format - "'Name's took a loan of X. The outstanding balance is Y".

Hint: If your `for`

loop is defined as:

```
for loan in loans :
...
```

You can use `loan[0]`

to access the first element, `loan[0]`

to access the second element and so on. Feel free to discuss the problem in the forum/comments.

#### Related Downloads

## Free Guides - Getting Started with R and Python

Enter your name and email address below and we will email you the guides for R programming and Python.