Iterating Lists in python

A person in an orange shirt works along a conveyor belt with boxes, apple, and envelope under text “Iterating Lists” on pale magenta.

Lists often hold many items, so we usually want to do the same action for each one: print it, total it, or transform it. Python makes this easy with loops that move through a list item by item.

The for loop: the everyday workhorse

A for loop steps through a list, giving you each item in turn. You don’t need to worry about positions or counters unless you want them.

Things to try

  • Make a list of three hobbies and print each one.
  • Create a list of numbers and print each number doubled.

Looping with indexes using range()

Sometimes you need the position as well as the value (for example, to replace an item by index). Use range(len(my_list)) to loop over positions.

Things to try

  • Print every second item in a list (use range(0, len(items), 2)).
  • Replace the middle item of a three-element list using its index.

Getting index and value with enumerate()

enumerate() gives you both the index and the item in one neat package, which reads nicer than range(len(...)).

You can also start counting from 1 for display:

Things to try

  • Print a numbered menu from a list of meal choices starting at 1.
  • Use enumerate() to find the index of a specific item and print it.

Using a while loop (when you control the steps)

A while loop is useful if you want fine control over the stopping condition or you plan to change the list as you go.

Be careful to update the index, or you’ll loop forever.

Things to try

  • Use a while loop to print items until you meet a certain value, then stop.
  • Walk through a list and build a new list containing only even numbers.

What is an iterator and why should you care?

An iterator is an object that remembers where it is in a sequence and knows how to give you the next item when asked. In Python, lists are iterables (things you can iterate over). Calling iter(my_list) gives you an iterator. The iterator’s job is to provide items one at a time and raise StopIteration when it runs out.

The for loop uses this protocol behind the scenes: it calls iter(...) to get an iterator, repeatedly calls next(...), and stops on StopIteration.

Things to try

  • Create an iterator from a list and call next() until it stops.
  • Write a small loop that uses iter() and next() manually (with try/except StopIteration) to print items.

Wrapping up

  • Use for for clean, readable loops over a list.
  • Add indexes with range() or use enumerate() for a clearer index–value pair.
  • while gives you manual control when needed.
  • An iterator is the mechanism that hands out items one by one; for uses it automatically.

Main Topic

Introducing Python Lists

A cartoon frog smiles while writing “Python Lists” with items flies, worms, insects on a chalkboard in a classroom setting.

This is a Python series introducing lists, their importance, and everyday uses. Light-hearted explanations make coding concepts simple, clear, and fun.

Other Tutorials in this Topic

A person’s hands holding pen and eraser above a notepad on a desk, crossing out “bread” from a shopping list.

Beginning Python Lists

A practical Python tutorial introducing lists, showing how to access, add, modify, and remove items with clear…

A cheerful goose sorts colourful books by size on a shelf, with bold text “Sorting Python Lists” against a pale magenta background.

Sorting Python Lists

A practical Python tutorial explaining how to sort lists in ascending and descending order, comparing `sort()` with…

A smiling blacksmith hammers words into metal blocks on an anvil, labelled “cat,” “7,” and “apple,” symbolising immutable Python tuples.

Tuples in Python

A Python tutorial explaining tuples: how to create them, their immutability, practical uses, adding to lists, and…

A cheerful cartoon monkey sorts bananas from a mixed fruit box into another box on a conveyor belt, with “Filtering Lists” text.

Maps and Filters in python

This tutorial explains how to use Python’s `map()` and `filter()` functions to transform and select list items,…