Welcome to The Coding College, where we make learning Python simple and engaging! Lists are one of the most versatile and essential data structures in Python. To truly master their use, practice is key.
This post provides a range of Python list exercises to help you solidify your understanding. These exercises are designed for beginners to advanced learners, covering basic list operations, slicing, comprehensions, and more.
Why Practice List Exercises?
Practicing Python list exercises helps you:
- Develop a strong foundation in data manipulation.
- Solve real-world problems involving data storage and organization.
- Prepare for coding interviews and Python programming challenges.
Python List Exercises
1. Create and Access Lists
Task:
- Create a list of your favorite fruits:
["apple", "banana", "cherry"]
. - Access the first and last elements.
Expected Output:
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # Output: apple
print(fruits[-1]) # Output: cherry
2. Modify List Items
Task:
- Replace
"banana"
with"orange"
in the list["apple", "banana", "cherry"]
.
Expected Output:
fruits = ["apple", "banana", "cherry"]
fruits[1] = "orange"
print(fruits) # Output: ["apple", "orange", "cherry"]
3. Add and Remove Items
Task:
- Add
"kiwi"
to the end of the list usingappend()
. - Remove
"apple"
usingremove()
.
Expected Output:
fruits = ["apple", "banana", "cherry"]
fruits.append("kiwi")
fruits.remove("apple")
print(fruits) # Output: ["banana", "cherry", "kiwi"]
4. Slice a List
Task:
- Extract the middle two items from the list
[1, 2, 3, 4, 5]
.
Expected Output:
numbers = [1, 2, 3, 4, 5]
print(numbers[1:4]) # Output: [2, 3, 4]
5. Sort and Reverse a List
Task:
- Sort the list
[3, 1, 4, 2]
in ascending order. - Reverse the sorted list.
Expected Output:
numbers = [3, 1, 4, 2]
numbers.sort()
print(numbers) # Output: [1, 2, 3, 4]
numbers.reverse()
print(numbers) # Output: [4, 3, 2, 1]
6. Count Elements
Task:
- Count the occurrences of
"cat"
in the list["cat", "dog", "cat", "bird"]
.
Expected Output:
animals = ["cat", "dog", "cat", "bird"]
count = animals.count("cat")
print(count) # Output: 2
7. Combine Two Lists
Task:
- Merge
[1, 2, 3]
and[4, 5, 6]
using the+
operator.
Expected Output:
list1 = [1, 2, 3]
list2 = [4, 5, 6]
combined = list1 + list2
print(combined) # Output: [1, 2, 3, 4, 5, 6]
8. Filter Even Numbers
Task:
- Use list comprehension to create a list of even numbers from
[1, 2, 3, 4, 5, 6]
.
Expected Output:
numbers = [1, 2, 3, 4, 5, 6]
evens = [num for num in numbers if num % 2 == 0]
print(evens) # Output: [2, 4, 6]
9. Find the Maximum Value
Task:
- Find the largest number in the list
[10, 20, 30, 40, 50]
.
Expected Output:
numbers = [10, 20, 30, 40, 50]
max_value = max(numbers)
print(max_value) # Output: 50
10. Nested Lists
Task:
- Access the number
2
in the nested list[[1, 2], [3, 4]]
.
Expected Output:
nested_list = [[1, 2], [3, 4]]
print(nested_list[0][1]) # Output: 2
Advanced Exercises
1. Flatten a Nested List
Task:
- Convert
[[1, 2], [3, 4]]
into[1, 2, 3, 4]
.
Hint: Use a list comprehension.
2. Remove Duplicates
Task:
- Remove duplicates from
[1, 2, 2, 3, 4, 4]
and return a unique list.
3. Find Common Elements
Task:
- Find common elements between
[1, 2, 3]
and[2, 3, 4]
.
4. Generate a Matrix
Task:
- Create a 3×3 matrix (list of lists) with all values set to
0
.
Why Practice with The Coding College?
At The Coding College, we provide hands-on exercises and real-world scenarios to help you apply Python skills effectively. Practicing these exercises will help you master Python lists and become a more confident programmer.
Conclusion
Python lists are incredibly versatile, and practicing these exercises will help you unlock their full potential. Whether you’re preparing for interviews, solving real-world problems, or sharpening your skills, these exercises are a great way to level up your Python expertise.