concept loop in category jupyter

appears as: loop, loop
Machine Learning for Business: Using Amazon SageMaker and Jupyter

This is an excerpt from Manning's book Machine Learning for Business: Using Amazon SageMaker and Jupyter.

6.3.1. Displaying columns of data with a loop

The loop is called a for loop, which means you give it a list (usually) and step through each item in the list. This is the most common type of loop you’ll use in data analysis and machine learning because most of the things you’ll loop through are lists of items or rows of data.

The standard way to loop is shown in the next listing. Line 1 of this listing defines a list of three items: A, B, and C. Line 2 sets up the loop, and line 3 prints each item.

To enumerate through a list, you provide two variables to store the information from the loop and to wrap the list you are looping through. The enumerate function returns two such variables. The first variable is the count of how many times you’ve looped (starting with zero), and the second variable is the item retrieved from the list. Listing 6.6 shows listing 6.4 converted to an enumerated for loop.

Listing 6.6. Standard way to enumerate through a loop
my_list = ['A', 'B', 'C']              #1
for i, item in enumerate(my_list):     #2
    print(f'{i}. {item}')              #3