[Python Basics] Working With Lists — Part 1

We practice how to loop over some lists using a few lines of code regardless of how long the lists are.

A Ydobon
3 min readDec 15, 2019
Photo by Lysander Yuen on Unsplash

We already know what the python list is and how to deal with it in the for loop. Do we?

For many of you, it is true. But there are still growing number of new generations who want to participate in this fascinating new world of machine learning, artificial intelligence, data science or whatever. We should provide them some materials to read, think and learn.

This post is an attempt to prepare teaching materials to my students whose major is not computer science, but who are willing to learn this new fascinating world.

Looping Over an Entire List

We often want to run over all entries in a list, performing the same task with each item. When we want to do this, we can use Python’s for loop.

my_string = "Hello, my Python!"  #1
my_list = list(my_string) #2
for item in my_list: #3
print(item.upper()) #4

At #1, we make a string. Then we make a list containing every letters, spaces and punctuation marks within the variable my_string at #2.

At #3 we define a for loop. This line tells Python to take an item in my_list and store it in the variable item. At #4, We tell Python to print the upper case letter in item. Python then repeats line #3 and #4, one for each item in the list.

A Closer Look at Looping

In some areas, computers perform a way better than human. The looping is one of them.

for item in my_list:

This code tells Python to retrieve the first element in the list my_list and store it in the variable named item. Note that the for statement always comes with in keyword in.

    print(item.upper())

Current value of variable item is ‘H’. This statement prints the upper case letter of the variable, which is the same as the original one. Now there are more elements in the list, Python goes back to the first line of the loop.

Doing More Work Within a for loop

Let’s build on the previous example by adding a new line. We can also include as many lines of code as we want in the for loop. Every indented line following the line for item in my_list is considered inside the loop, and each indented line is executed once for each value in the list. Therefore you can do as much work as you like with each value in the list.

my_string = "Hello, my Python!"  #1
my_list = list(my_string) #2
for item in my_list: #3
print(item.upper()) #4
print("processed") #5

Inside the for loop, #4 and #5 will be executed sequentially, and then it goes up the line #3.

Doing Something After a for loop

What happens once a for loop has finished?

my_string = "Hello, my Python!"  #1
my_list = list(my_string) #2
for item in my_list: #3
print(item.upper()) #4
print("Finally, I'm done.") #5

The line #5 will be executed after the for loop has finished.

Common Errors

Python used indentation to determine when one line of code is connected to the line above.

Forgetting the Colon

This is probably the most common error when we try to use the for loop for the first time.

my_string = "Hello, my Python!"  #
my_list = list(my_string) #
for item in my_list # Colon : is missing here.
print(item.upper()) #

Forgetting to Indent

Always indent the line after the for statement in a loop. It tells Python that we are going to start a new block.

my_string = "Hello, my Python!"  #
my_list = list(my_string) #
for item in my_list: #
print(item.upper()) # We need spaces.

Forgetting to Indent Additional lines or Indenting Unnecessarily After the Loop

The following example might be right or wrong depending on what you intend to do. Think about it as an exercise!!!

my_string = "Hello, my Python!"  #
my_list = list(my_string) #
for item in my_list: #
print(item.upper()) #
print('Done') # We need spaces here too or not.

--

--