Chapter 7: Organizing Your Data with Lists

So far, we've worked with variables that hold a single piece of information, like a number, a string, or a boolean. This is great, but what happens when you need to work with a collection of items? For example, a list of student names, a sequence of temperatures, or your grocery shopping list.

Storing each one in a separate variable would be a nightmare:

student1 = "Alice"
student2 = "Bob"
student3 = "Charlie"
# ...and so on.

This is not practical. For these situations, Python gives us a wonderful data structure called a list. A list is an ordered, changeable collection of items.

What is a List?

You can think of a list like a numbered series of boxes, where each box can hold a value.

  • Ordered: The items in a list have a specific order, and that order will stay the same unless you change it.

  • Changeable (Mutable): You can add, remove, and change items in a list after it has been created.

  • Can Contain Anything: A list can hold items of any data type, and you can even mix different types in the same list.

Creating and Accessing Lists

You create a list by placing comma-separated values inside square brackets [].

# A list of strings
shopping_list = ["apples", "bread", "milk", "eggs"]

# A list of numbers
prime_numbers = [2, 3, 5, 7, 11, 13]

# A list with mixed data types
mixed_list = ["hello", 100, True, 3.14]

# An empty list
empty_list = []

print(shopping_list)
print(prime_numbers)

Accessing Items with Indexes

To get a single item from a list, you use its index. An index is the item's position in the list. In Python, indexing starts at 0, not 1.

You can also use negative indexes to count from the end of the list. [-1] gets the last item, [-2] gets the second-to-last, and so on.

Modifying Lists

Because lists are mutable, you can change them whenever you want.

Changing an Item

You can change an item at a specific index using assignment:

Adding Items

  • .append(item): Adds an item to the very end of the list. This is the most common way to add items.

  • .insert(index, item): Inserts an item at a specific position.

Removing Items

  • del list_name[index]: Deletes the item at a specific index.

  • .pop(index): Removes the item at a specific index and lets you use it. If you don't provide an index, it removes and returns the last item.

  • .remove(value): Removes the first item in the list that matches a specific value.

Looping Through a List (The Best Part!)

This is where the power of lists and loops comes together. You can use a for loop to perform an action on every single item in a list.

This is much cleaner than accessing each student by index! This pattern of using a for loop to iterate over a list is one of the most common and useful things you will do in Python.

Other Useful List Functions and Methods

  • len(list_name): Get the number of items in a list.

  • .sort(): Sorts the list in place (ascending order by default).

  • .reverse(): Reverses the order of the list in place.

Summary and What's Next

You've just been introduced to lists, a fundamental tool for organizing data in Python.

  • Lists are ordered collections of items, created with square brackets [].

  • You access items using zero-based indexes, like my_list[0].

  • Lists are mutable: you can .append(), .insert(), .remove(), and change items.

  • Combining lists with for loops is a powerful way to process collections of data.

You now have a way to store and manage groups of related information. In the next chapters, we will explore other ways to structure data, such as tuples (which are like lists but cannot be changed) and dictionaries (which let you store data as key-value pairs).

Practice Time!

  1. Create a list of your three favorite movies.

  2. Print the second movie in the list.

  3. Add a fourth movie to the end of the list using .append().

  4. Use a for loop to print the phrase "I love watching [Movie Name]!" for each movie in your list.

Last updated