Python Grids Explained
Python Lists
Organizing Data with Lists
In Python, one of the most common ways to group related items is by using a list. Think of a list like a grocery list or a to-do list in the real world. It's a single place where you can store multiple items in a specific order.
You create a list by placing items inside square brackets [], separated by commas. Lists can hold different types of data, like numbers or strings.
# A list of numbers
prime_numbers = [2, 3, 5, 7, 11]
# A list of strings
tasks = ["Call mom", "Buy milk", "Go to gym"]
Lists are the workhorse data structure for Python.
Accessing and Modifying Elements
The order of items in a list matters. Each item has a specific position, called an index. The trick is that Python starts counting from 0, not 1. So, the first item is at index 0, the second is at index 1, and so on.
You can grab an item from a list by using its index in square brackets.
tasks = ["Call mom", "Buy milk", "Go to gym"]
# Get the first item (index 0)
first_task = tasks[0]
print(first_task) # Output: Call mom
# Get the third item (index 2)
third_task = tasks[2]
print(third_task) # Output: Go to gym
You can also use negative indexes to count from the end of the list. Index -1 refers to the last item, -2 to the second-to-last, and so on. This is handy when you don't know how long the list is.
tasks = ["Call mom", "Buy milk", "Go to gym"]
# Get the last item
last_task = tasks[-1]
print(last_task) # Output: Go to gym
Because lists are mutable, meaning they can be changed, you can update an item by assigning a new value to its index.
tasks = ["Call mom", "Buy milk", "Go to gym"]
# Change the second item
tasks[1] = "Buy almond milk"
print(tasks)
# Output: ['Call mom', 'Buy almond milk', 'Go to gym']
Common List Operations
Often, you'll need to add or remove items from a list. Python gives you simple commands, called methods, to do this.
To add an item to the very end of a list, use the append() method.
tasks = ["Call mom", "Buy milk"]
# Add a new task to the end
tasks.append("Schedule dentist appointment")
print(tasks)
# Output: ['Call mom', 'Buy milk', 'Schedule dentist appointment']
To remove an item, you have a couple of options. If you know the exact value of the item you want to remove, use the remove() method. It finds the first matching item and deletes it.
tasks = ["Call mom", "Buy milk", "Go to gym"]
tasks.remove("Buy milk")
print(tasks)
# Output: ['Call mom', 'Go to gym']
If you want to remove an item by its position, or if you want to use the removed item, the pop() method is better. By default, pop() removes and returns the last item from the list.
tasks = ["Call mom", "Buy milk", "Go to gym"]
# Remove and get the last item
completed_task = tasks.pop()
print(completed_task)
# Output: Go to gym
print(tasks)
# Output: ['Call mom', 'Buy milk']
You can also give pop() an index to remove an item from a specific position.
Finally, to find out how many items are in a list, you can use the built-in
len()function. It's not a method, so you writelen(my_list)instead ofmy_list.len().
tasks = ["Call mom", "Buy milk"]
list_length = len(tasks)
print(list_length)
# Output: 2
Let's review these core concepts.
Time to check your understanding.
How do you create a new, empty list named my_list in Python?
Given the list colors = ['red', 'green', 'blue', 'yellow'], what code would access the item 'blue'?
Lists are a foundational part of Python that you'll use constantly, especially when working with grids or collections of data.