Salvatore Lopiparo

Software Engineer

Super Simple Python #7 – The for Loop

Last time we learned about lists and how to manipulate the list itself. This time we’ll learn how to do things to items in the list with the for statement.

If this is your first time here, I recommend looking at the first lesson. Starting there and going through the rest of the lessons will prepare you to go through this more advanced lesson!

First, let’s make a list we can do things to.

>>> grocery_list = ['milk', 'apples', 'orange juice', 'paper towels', 'bbq sauce', 'eggs']
>>> print(grocery_list)
['milk', 'apples', 'orange juice', 'paper towels', 'bbq sauce', 'eggs']

Trying to do your shopping with the items listed like that would be quite difficult. Let’s try printing them in a better format using the for statement!

>>> for grocery_item in grocery_list:
...     print('- ' + grocery_item)
...     
- milk
- apples
- orange juice
- paper towels
- bbq sauce
- eggs

Pretty simple, yeah? When reading the lines above, we say “For each grocery_item in grocery_list, do some stuff (in this case, print the item). The word after for is the variable used for each item in the list, one after another. Using the for statement saves you from writing a lot of extra code, and handles lists of different sizes:

>>> print('- ' + grocery_list[0])
- milk
>>> print('- ' + grocery_list[1])
- apples
>>> print('- ' + grocery_list[2])
- orange juice
>>> print('- ' + grocery_list[3])
- paper towels
>>> # and so on...

We can make something more interesting too. Let’s say we have a bank account and a list of transactions.

>>> account_balance = 1000
>>> transactions = [-3, -149, -3, -3, -12, -3, -699, -3]

Now we can report each transaction and subtract it from our account balance using a for statement:

>>> account_balance = 1000
>>> for transaction in transactions:
...     print("Transaction processed: " + str(transaction))
...     account_balance = account_balance + transaction
...     print("Remaining Balance: " + str(account_balance))
...     
Transaction processed: -3
Remaining Balance: 997
Transaction processed: -149
Remaining Balance: 848
Transaction processed: -3
Remaining Balance: 845
Transaction processed: -3
Remaining Balance: 842
Transaction processed: -12
Remaining Balance: 830
Transaction processed: -3
Remaining Balance: 827
Transaction processed: -699
Remaining Balance: 128
Transaction processed: -3
Remaining Balance: 125

Super Simple Python #6 – Lists

Lists in Python are the same thing as they are in every day language: A sequence of items that go together. Usually things in the list are of the same type, like a grocery list or a hit list.

If this is your first time here, I recommend looking at the first lesson. Starting there and going through the rest of the lessons will prepare you to go through this more advanced lesson!

You can either define an empty list:

>>> grocery_list = []

Or define a list with things in it:

>>> hit_list = ['Frankie', 'Jimmy', 'Gambini', 'Capone', 'Luciana', '3-Fingers Tony', 'Corleone']

If you want to get a specific item in the list, use square brackets and the index of the item. Note that lists start with an index of 0, not 1.

>>> hit_list[0]
'Frankie'
>>> hit_list[1]
'Jimmy'
>>> hit_list[5]
'3-Fingers Tony'

Now let’s say we need to… ahemremove someone from the list.

>>> hit_list.remove('Gambini')
>>> hit_list
['Frankie', 'Jimmy', 'Capone', 'Luciana', '3-Fingers Tony', 'Corleone']

Or, if you prefer to work in order, you can pop them off the back…

>>> hit_list.pop()
'Corleone'
>>> hit_list
['Frankie', 'Jimmy', 'Capone', 'Luciana', '3-Fingers Tony']

or the front…

>>> hit_list.pop(0)
'Frankie'
>>> hit_list
['Jimmy', 'Capone', 'Luciana', '3-Fingers Tony']

If you want to change an item in the list, simply assign it to an index like you would any other variable.

>>> hit_list[3]
'3-Fingers Tony'
>>> hit_list[3] = "2-Fingers Tony"
>>> hit_list[3]
'2-Fingers Tony'

I’d rather not think about why his name changed.

Let’s say some guy on the internet is making death threats to several high-profile gangsters. Obviously we will need to add someone to the list. You can append them to the end of the list like so:

>>> hit_list.append('Salvatore')
>>> hit_list
['Jimmy', 'Capone', 'Luciana', '2-Fingers Tony', 'Salvatore']

Or, if you’d rather not wait to elimina… I mean put them at the end of the list, we can use insert, passing it the index we want to insert it before. Inserting before 0 will put it in the front.

>>> hit_list.remove('Salvatore')
>>> hit_list.insert(0, 'Salvatore')
>>> hit_list
['Salvatore', 'Jimmy', 'Capone', 'Luciana', '2-Fingers Tony']

I will have to cut this lesson here, as there appears to be a couple of violin salesmen at the door.