Why does it print the entire list each time my loop runs?


#1

Question

Why does it print the entire list each time my loop runs?

Answer

If you print the name of your list each time, instead of printing the variable, called item in the example, then that’s why. Take a look at the code below:

animals = ["cat", "doge", "liger"]
for animal in animals:
  print animals

I’ve told it to print animals, rather than each individual animal, which I told it to store in the singular animal variable after the word for.


FAQ: Learn Python: A Day at the Supermarket - BeFOR We Begin