Looping through data
1. Looping through Python data structures
In this lesson, you are going to learn about using for loops in Python. While the basic concept is similar to what you are familiar with in MATLAB, we are going to cover some of the differences, both in syntax and function. You will learn how to loop through some of the common data structures you’ve already learned to work with: lists, dictionaries, NumPy arrays, and pandas DataFrames. Let’s get started.2. How for loops work in Python
On the left, we have an example of a simple for-loop in MATLAB, which iterates through the integers 1 through 5 and displays them on the console. On the right is a for-loop in Python which does the same. The first difference we see is the syntax for assigning values to the variable you are looping. In Python, you designate the items you are assigning to i with “in” then an iterator with the items you want to loop over. In this example, our iterator is a list of integers. Next, the for line terminates with a colon and the next line is indented. It's these two things, the colon and the indentation, which indicate that the “print(i)” expression is in the for-loop. The for loop is terminated as soon as we unindent. Besides the differences in syntax, there's an important functional difference here, too. Whereas in MATLAB, you only loop over integers, in Python, you can loop over items of any type. Let me show you what I mean with an example.3. Looping through a list of strings
Let’s say we have a list of groceries and we want to loop through the list and print them. We start with a list of strings assigned to the variable "groceries." Then we loop directly over the list with “for item in groceries:” and Python will assign each item in groceries to the item variable on each pass through the for loop. Unlike in MATLAB, there’s no need for you to keep track of the index in the list. This approach of looping through items not only works with lists, but it also works with lots of other Python data structures you know, including dictionaries and numpy arrays.4. Looping through a dictionary
We can also loop through the items in a dictionary. Here, we have a Python dictionary of state names and their corresponding abbreviations stored in the variable “abbreviations.” We can iterate through the state, abbreviation pairs in this dictionary by calling the items() method on the dictionary. Now, let’s look at NumPy arrays.5. Looping through a 1D NumPy array
For 1-D numpy arrays, looping over the array is similar to what we would expect from a list. The for loop in this example assigns each element in the array to the “element” variable.6. Looping through a 2D NumPy array
For 2-D numpy arrays, it’s a little bit different. When we loop over a 2D NumPy array, we loop over the first axis of the array, the rows. Here, each row in the array gets assigned to the variable “row.”7. Looping through pandas DataFrames
Finally, let's see how to loop through pandas DataFrames. We loop through the rows in a pandas DataFrame by calling the iterrows() method on the DataFrame. This method creates an iterator that returns two items on each loop: the index of each row, and each row as a pandas Series object.8. Let's practice!
Now, use the exercises to get some practice looping through lists, numpy arrays, dictionaries, and DataFrames.Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.