Welcome back! Yesterday, we learned about working with files in Python. Today, we’ll dive into lists and list comprehensions, which are powerful tools for working with collections of data. By the end of this day, you’ll know how to create, manipulate, and use lists efficiently. Let’s get started!
What are Lists?
Lists are ordered collections of items that can be of different data types. They are mutable, meaning you can change their content after creation.
Creating Lists
You can create a list by placing comma-separated values inside square brackets:
fruits = ["apple", "banana", "cherry"]
numbers = [1, 2, 3, 4, 5]
mixed_list = [1, "apple", True, 3.14]
Accessing List Elements
You can access list elements using their index. Remember, Python uses zero-based indexing.
print(fruits[0]) # Output: apple
print(fruits[1]) # Output: banana
print(fruits[-1]) # Output: cherry (last element)
Modifying Lists
You can modify list elements by assigning a new value to a specific index.
fruits[1] = "blueberry"
print(fruits) # Output: ["apple", "blueberry", "cherry"]
Adding Elements
You can add elements to a list using the append() and insert() methods.
fruits.append("date")
print(fruits) # Output: ["apple", "blueberry", "cherry", "date"]
fruits.insert(1, "banana")
print(fruits) # Output: ["apple", "banana", "blueberry", "cherry", "date"]
Removing Elements
You can remove elements from a list using the remove(), pop(), and del methods.
fruits.remove("banana")
print(fruits) # Output: ["apple", "blueberry", "cherry", "date"]
popped_fruit = fruits.pop()
print(popped_fruit) # Output: date
print(fruits) # Output: ["apple", "blueberry", "cherry"]
del fruits[0]
print(fruits) # Output: ["blueberry", "cherry"]
List Comprehensions
List comprehensions provide a concise way to create lists. They are more readable and efficient than traditional for loops.
Basic syntax:
new_list = [expression for item in iterable if condition]
Example:
numbers = [1, 2, 3, 4, 5]
squares = [x**2 for x in numbers]
print(squares) # Output: [1, 4, 9, 16, 25]
# Using a condition
even_squares = [x**2 for x in numbers if x % 2 == 0]
print(even_squares) # Output: [4, 16]
Nested Lists
Lists can contain other lists, creating a nested structure. You can access elements in nested lists using multiple indices.
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print(matrix[0][1]) # Output: 2
print(matrix[2][0]) # Output: 7
Practice Time!
Let’s put what we’ve learned into practice. Write a Python program that creates, modifies, and manipulates lists using list comprehensions and nested lists.
# Creating a list
colors = ["red", "green", "blue", "yellow"]
# Accessing elements
print(colors[0]) # Output: red
print(colors[-1]) # Output: yellow
# Modifying elements
colors[1] = "purple"
print(colors) # Output: ["red", "purple", "blue", "yellow"]
# Adding elements
colors.append("orange")
colors.insert(2, "cyan")
print(colors) # Output: ["red", "purple", "cyan", "blue", "yellow", "orange"]
# Removing elements
colors.remove("purple")
print(colors) # Output: ["red", "cyan", "blue", "yellow", "orange"]
popped_color = colors.pop(1)
print(popped_color) # Output: cyan
print(colors) # Output: ["red", "blue", "yellow", "orange"]
# List comprehension
numbers = [1, 2, 3, 4, 5]
squares = [x**2 for x in numbers if x % 2 == 1]
print(squares) # Output: [1, 9, 25]
# Nested lists
nested_list = [[1, 2], [3, 4], [5, 6]]
flattened = [num for sublist in nested_list for num in sublist]
print(flattened) # Output: [1, 2, 3, 4, 5, 6]
Conclusion
Great job today! You’ve learned how to create and manipulate lists and use list comprehensions in Python. Tomorrow, we’ll dive into dictionaries and sets, which are other powerful data structures in Python. Keep practicing and having fun coding!
Learn Python with Me
Join me, Andrew, as we continue this exciting Python adventure. I’ll be sharing my progress and tips, and I hope you’ll share yours too. Let’s code together and have some fun!
By following this plan, you’re not only learning Python but also building a solid foundation in programming. Happy coding!
Leave a Reply