Welcome back! In this section, we'll dive into one of Python's most fundamental and versatile data structures: lists. Think of a list as a container where you can store a collection of items. What makes lists special is that they are 'ordered' and 'mutable'.
'Ordered' means that the items in a list have a specific sequence, and this sequence will not change unless you explicitly modify it. Each item in the list has an 'index', which is its position. Indices in Python start from 0 for the first item, 1 for the second, and so on.
'Mutable' means that you can change, add, or remove items from a list after it has been created. This flexibility is incredibly powerful for managing data dynamically.
Let's see how to create a list:
fruits = ["apple", "banana", "cherry"]
numbers = [1, 2, 3, 4, 5]
mixed_list = ["hello", 10, True, 3.14]
empty_list = []As you can see, lists can hold items of the same data type (like fruits and numbers) or different data types (like mixed_list). An empty list can also be created.
Accessing items in a list is done using their index. You use square brackets [] with the index number inside.
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # Output: apple
print(fruits[1]) # Output: bananaPython also supports negative indexing, where -1 refers to the last item, -2 to the second-to-last, and so on. This is a convenient way to access elements from the end of the list.
fruits = ["apple", "banana", "cherry"]
print(fruits[-1]) # Output: cherry
print(fruits[-2]) # Output: bananaWhat happens if you try to access an index that doesn't exist? You'll get an IndexError.
fruits = ["apple", "banana", "cherry"]
# print(fruits[3]) # This would cause an IndexErrorSince lists are mutable, we can change individual items. Simply assign a new value to the desired index.
fruits = ["apple", "banana", "cherry"]
fruits[1] = "blueberry"
print(fruits) # Output: ['apple', 'blueberry', 'cherry']We can also add items to a list. Python provides several methods for this:
The append() method adds an item to the end of the list.
fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
print(fruits) # Output: ['apple', 'banana', 'cherry', 'orange']The insert() method adds an item at a specified index.
fruits = ["apple", "banana", "cherry"]
fruits.insert(1, "grape")
print(fruits) # Output: ['apple', 'grape', 'banana', 'cherry']Removing items from a list is also straightforward:
The remove() method removes the first occurrence of a specified value.
fruits = ["apple", "banana", "cherry", "banana"]
fruits.remove("banana")
print(fruits) # Output: ['apple', 'cherry', 'banana']The pop() method removes and returns the item at a specified index. If no index is specified, it removes and returns the last item.
fruits = ["apple", "banana", "cherry"]
removed_fruit = fruits.pop(1)
print(fruits) # Output: ['apple', 'cherry']
print(removed_fruit) # Output: bananaThe del keyword can be used to remove an item at a specific index or even delete the entire list.
fruits = ["apple", "banana", "cherry"]
del fruits[0]
print(fruits) # Output: ['banana', 'cherry']
del fruits # This would delete the entire listYou can also get the length of a list using the len() function.
fruits = ["apple", "banana", "cherry"]
print(len(fruits)) # Output: 3graph TD;
A[Start with a list]
B{Accessing items using index}
C{Modifying items}
D{Adding items (append, insert)}
E{Removing items (remove, pop, del)}
F[End]
A --> B
A --> C
A --> D
A --> E
B --> F
C --> F
D --> F
E --> F
Lists are a fundamental building block in Python. Mastering them will open up a world of possibilities for data manipulation and organization. In the next section, we'll explore another important ordered data structure: tuples.