What can we do with each item in a list in a for loop?


#1

Question

What can we do with each item in a list in a for loop?

Answer

For loops are great for doing something to every item in a list. So good, in fact, that they built this handy syntax into the language itself! In other programming languages, doing this for item in list is not so simply written!
In this exercise, that something we want to do each time is to multiply the item by 2. But you could do whatever you wanted to each item each time it loops.
To demonstrate how item becomes the next item in the list after every time it loops, take a look at the code below:

my_list = [1, 2, 3, 4]

for item in my_list:
  print item  # displays the current item in the list

This will print:

1
2
3
4

FAQ: Learn Python: Python Lists and Dictionaries - For One and All