Skip to main content
Welcome back. You probably know how to repeat actions with loops (for example, prompting for a password until it’s correct). But when you have a collection of items — daily goals, shopping items, or any group of values — lists let you walk through those items one at a time and perform operations on each. This article teaches Python lists: how to create them, access and modify items, loop through them, and use common list methods. By the end of this lesson you’ll be able to:
  • create lists and access items by index,
  • add and remove items,
  • loop through lists with for loops,
  • and use lists to store and process collections of data.
Create a new script called lists.py and follow along.

What is a list?

A list is a mutable sequence that can store multiple items. Use square brackets to define a list:
Lists can hold mixed types:
This flexibility (storing different types together) distinguishes Python lists from lower-level arrays in some other languages. Lists are general-purpose, can be changed in-place, and are easy to iterate over.

Accessing items by index

Consider a shopping list:
Each item has an index (position). Indexes start at 0:
  • shopping[0] -> "Eggs"
  • shopping[1] -> "Bread"
  • shopping[2] -> "Milk"
  • shopping[3] -> "Butter"
Example:
Expected output:

Negative indexing

You can count from the end using negative indexes. -1 is the last item, -2 the second-to-last, and so on:
To print "Milk" you can use either:

Modifying items

Assign a new value to an index to overwrite an item:

Adding and removing items

Use append() to add to the end, remove() to delete by value, and pop() to remove by index and return the removed item.
Note: remove() takes a value and deletes the first matching element; pop() takes an index and returns the removed element.

Common list operations

  • len(my_list) — number of items in the list.
  • item in my_list — membership test (case-sensitive for strings).
  • Loop with for item in my_list: to process each element.
Example:
Be aware that membership checks are case-sensitive:
To perform a case-insensitive membership test, normalize strings first:
A code editor window (showing Python code to collect goals and a terminal) fills most of the screen. A man in a black t‑shirt stands on the right, gesturing toward the code.

Quick reference — list methods and operations

For full details, see the official Python lists documentation: https://docs.python.org/3/tutorial/datastructures.html

Putting it together: a simple goals tracker

Let’s build a small program that:
  • collects goals from the user until they type done,
  • shows the total number of goals,
  • prompts the user to mark which goals are complete,
  • displays completed and remaining counts and a final message.
Save this as lists.py:
Example run (user input shown after prompts):

Recap

  • Lists are defined with square brackets and can store multiple items, including mixed types.
  • Access list items by zero-based index or by negative index to count from the right.
  • Modify lists with index assignment, append(), remove(), and pop().
  • Use len() for the count and in for membership tests.
  • Iterate with for item in my_list: to process each element.
  • Normalize strings (for example, with .lower()) when checking user input or doing case-insensitive comparisons.
For further reading:
Tip: When checking user input (like y/n) or looking for items in a list, normalize strings with .lower() to avoid case-sensitivity issues.

Watch Video

Practice Lab