In programming, we often need to work with collections of items. Imagine keeping track of student names in a class, grocery items to buy, or high scores in a game. While you could create a separate variable for each item (like item1 = "milk"
, item2 = "eggs"
), this quickly becomes unmanageable as the number of items grows.
Python provides several built-in data structures, often called collections or containers, designed specifically for storing groups of data. One of the most fundamental and versatile is the list.
Think of a list as an ordered sequence of items, much like a shopping list or a numbered to-do list. Each item in the list has a specific position. Importantly, lists in Python are mutable, which means you can change their contents after they are created – you can add new items, remove existing ones, or change the value of items already in the list.
You create a list in Python by enclosing a comma-separated sequence of items within square brackets []
.
# An empty list
empty_list = []
print(empty_list)
# A list of integers
numbers = [1, 2, 3, 5, 8]
print(numbers)
# A list of strings
fruits = ["apple", "banana", "cherry"]
print(fruits)
# A list with mixed data types
mixed_list = [10, "hello", 3.14, True]
print(mixed_list)
As you can see, lists are flexible and can hold items of different data types, including numbers, strings, booleans, and even other lists (though we'll explore nested lists later).
Because lists are ordered, you can access individual items using their position, called an index. Python uses zero-based indexing, meaning the first item is at index 0, the second item is at index 1, and so on.
To access an item, you use the list name followed by the index in square brackets.
fruits = ["apple", "banana", "cherry", "date"]
# Access the first item (index 0)
first_fruit = fruits[0]
print(first_fruit) # Output: apple
# Access the third item (index 2)
third_fruit = fruits[2]
print(third_fruit) # Output: cherry
Attempting to access an index that doesn't exist (e.g., fruits[4]
in the list above, which only has indices 0, 1, 2, 3) will result in an IndexError
.
Python also supports negative indexing, which is a convenient way to access items starting from the end of the list. Index -1
refers to the last item, -2
refers to the second-to-last item, and so on.
fruits = ["apple", "banana", "cherry", "date"]
# Access the last item
last_fruit = fruits[-1]
print(last_fruit) # Output: date
# Access the second-to-last item
second_last_fruit = fruits[-2]
print(second_last_fruit) # Output: cherry
You can easily find out how many items are in a list using the built-in len()
function.
numbers = [10, 20, 30, 40, 50]
fruits = ["apple", "banana"]
empty_list = []
print(len(numbers)) # Output: 5
print(len(fruits)) # Output: 2
print(len(empty_list)) # Output: 0
Lists are a fundamental tool for organizing data in Python. Their key characteristics are:
In the next section, we'll explore the mutable nature of lists further and learn how to add, remove, and change items within them.
© 2025 ApX Machine Learning