How to loop over python dictionaries and Numpy arrays

Premium

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.

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

Unlock Premium Content

Upgrade your account to access the full article, downloads, and exercises.

You'll get access to:

  • Access complete tutorials and examples
  • Download source code and resources
  • Follow along with practical exercises
  • Get in-depth explanations