Python: Storing and Processing Data in Lists
This tutorial demonstrates how to store and process data in Python lists. Lists are versatile, mutable, and fundamental to many Python programs.
Example 1: Creating and Accessing Lists
Let’s start by creating a simple list of numbers and learning how to access its elements.
# Create a list of numbers
numbers = [1, 2, 3, 4, 5]
# Access elements using their index (starting from 0)
print("First element:", numbers[0])
print("Third element:", numbers[2])
print("Last element:", numbers[-1]) # Accessing the last element
# Slicing a list
print("Elements from index 1 to 3:", numbers[1:4])
#Adding an element
numbers.append(6)
print("List after appending 6:", numbers)
In this example, we created a list named `numbers`. We used square brackets `[]` to define the list. Each element is accessed using its index, which starts at 0. The last element can be accessed using negative indexing (e.g., `-1` refers to the last element). We also demonstrated slicing which extracts a portion of the list between the given indices. Finally, we added an element to the list using the `.append()` method.
Common Mistake: Trying to access an index that is out of bounds (e.g., `numbers[5]` when the list only has 5 elements) will raise an `IndexError`.
Correction: Always check that your index is within the valid range of the list (0 to length – 1) before accessing an element.
Output 1
First element: 1
Third element: 3
Last element: 5
Elements from index 1 to 3: [2, 3, 4]
List after appending 6: [1, 2, 3, 4, 5, 6]
Example 2: Processing List Data with Loops
Now, let’s iterate through the list and perform some operations on each element.
# Create a list of names
names = ["Alice", "Bob", "Charlie"]
# Iterate through the list using a for loop
for name in names:
print("Hello,", name + "!")
# Calculate the sum of the numbers in the 'numbers' list
total = 0
for number in numbers:
total = total + number
print(f"Current sum: {total}")
#Calculate the average
average = total / len(numbers)
print(f"Average: {average}")
Here, we used a `for` loop to iterate through each `name` in the `names` list. Inside the loop, we printed a greeting that includes the name. We also calculated the sum of the `numbers` list using another loop and added it to `total`. Then, we calculated the average by dividing `total` by the length of the `numbers` list. The f-string format is used to print the values during the summing process, showing intermediate results.
Output 2
Hello, Alice!
Hello, Bob!
Hello, Charlie!
Current sum: 1
Current sum: 3
Current sum: 6
Current sum: 10
Current sum: 15
Average: 3.0
Example 3: Modifying Lists
Lists are mutable, meaning you can change their contents after they’ve been created. Let’s demonstrate some common modifications.
# Create a list of temperatures in Celsius
temperatures_celsius = [25, 30, 20, 28]
# Convert temperatures to Fahrenheit
temperatures_fahrenheit = []
for temp_celsius in temperatures_celsius:
temp_fahrenheit = (temp_celsius 9/5) + 32
temperatures_fahrenheit.append(temp_fahrenheit)
print("Celsius Temperatures:", temperatures_celsius)
print("Fahrenheit Temperatures:", temperatures_fahrenheit)
#Modify the list in place
temperatures_celsius[0] = 27
print("Modified Celsius Temperatures:", temperatures_celsius)
In this example, we first created a list of Celsius temperatures. We then converted each temperature to Fahrenheit using the standard formula: `(Celsius 9/5) + 32`. We appended the Fahrenheit values to a new list called `temperatures_fahrenheit`. Finally, we modified the original `temperatures_celsius` list by changing the first element directly.
Output 3
Celsius Temperatures: [25, 30, 20, 28]
Fahrenheit Temperatures: [77.0, 86.0, 68.0, 82.4]
Modified Celsius Temperatures: [27, 30, 20, 28]



Leave a Reply