No history yet

Introduction to Python Lists

Organizing Data with Lists

In programming, we often need to group related items together. A Python list is perfect for this. It's an ordered collection of items, much like a grocery list or the numbered steps in a recipe. You can put almost anything in a list, including numbers, strings, and even other lists.

Think of a list as a train. Each car holds one item, and the cars are arranged in a specific sequence.

Creating a list is straightforward. You just enclose a comma-separated sequence of items in square brackets [].

prime_numbers = [2, 3, 5, 7, 11, 13]
planets = ['Mercury', 'Venus', 'Earth', 'Mars']
mixed_list = [42, 'hello', 3.14]

Accessing and Modifying Items

Each item in a list has a specific position, called an index. Python uses zero-based indexing, which means the first item is at index 0, the second is at index 1, and so on. To access an item, you use its index in square brackets.

planets = ['Mercury', 'Venus', 'Earth', 'Mars']

# Get the first planet (index 0)
first_planet = planets[0]  # Returns 'Mercury'

# Get the third planet (index 2)
third_planet = planets[2]   # Returns 'Earth'

You can also use negative indices to count from the end of the list. Index -1 refers to the last item, -2 to the second to last, and so on.

# Get the last planet
last_planet = planets[-1] # Returns 'Mars'

Lists are mutable, which means you can change them after they've been created. You can add, remove, or change items. A common way to add an item to the end of a list is with the .append() method.

planets.append('Jupiter')
# Now planets is ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter']

To remove an item, you can use .remove() if you know the value, or .pop() if you know the index.

# Remove by value
planets.remove('Venus')
# planets is now ['Mercury', 'Earth', 'Mars', 'Jupiter']

# Remove by index (and get the removed item)
removed_planet = planets.pop(1) # Removes 'Earth'
# planets is now ['Mercury', 'Mars', 'Jupiter']

Slicing and Dicing

Sometimes you need just a part of a list, not the whole thing. This is called slicing. You can specify a start and end index to create a new list containing just those elements. The slice goes up to, but does not include, the end index.

numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

# Get items from index 2 up to index 5
subset = numbers[2:5] # Returns [2, 3, 4]

# If you omit the start index, it defaults to 0
first_three = numbers[:3] # Returns [0, 1, 2]

# If you omit the end index, it goes to the end
from_five_on = numbers[5:] # Returns [5, 6, 7, 8, 9]

Slicing is a powerful way to work with portions of your data without affecting the original list.

Working Through a List

A common task is to perform an action on every item in a list. You can do this easily with a for loop. The loop will go through the list one item at a time, assigning each item to a temporary variable.

colors = ['red', 'green', 'blue']

for color in colors:
    print(f"The color is {color}")

# Output:
# The color is red
# The color is green
# The color is blue

This process of going through a collection of items is called iteration. It's a fundamental concept you'll use constantly when working with lists and other data structures in Python.

Let's check your understanding of these core list operations.

Quiz Questions 1/6

How do you create an empty list in Python named my_list?

Quiz Questions 2/6

Consider the list letters = ['a', 'b', 'c', 'd', 'e']. What does letters[-2] return?