Practicing Python list exercises is the best way to improve your coding skills. Lists are used everywhere in real-world Python projects like data processing, web development, and automation.
This tutorial includes basic to intermediate list exercises with solutions.
🔹 Exercise 1: Create a List
Task:
Create a list of 5 numbers and print it.
Solution:
numbers = [10, 20, 30, 40, 50]
print(numbers)
Output:
[10, 20, 30, 40, 50]
🔹 Exercise 2: Access List Items
Task:
Print the first and last item from a list.
Solution:
fruits = ["apple", "banana", "mango", "cherry"]
print(fruits[0])
print(fruits[-1])
Output:
apple
cherry
🔹 Exercise 3: Change List Item
Task:
Change the second item in a list.
Solution:
colors = ["red", "green", "blue"]
colors[1] = "yellow"
print(colors)
Output:
['red', 'yellow', 'blue']
🔹 Exercise 4: Add Items to List
Task:
Add a new item to the list.
Solution:
cars = ["BMW", "Toyota"]
cars.append("Honda")
print(cars)
Output:
['BMW', 'Toyota', 'Honda']
🔹 Exercise 5: Remove Item from List
Task:
Remove a specific item.
Solution:
fruits = ["apple", "banana", "mango"]
fruits.remove("banana")
print(fruits)
Output:
['apple', 'mango']
🔹 Exercise 6: Loop Through a List
Task:
Print all items in a list.
Solution:
numbers = [1, 2, 3, 4, 5]
for num in numbers:
print(num)
Output:
1
2
3
4
5
🔹 Exercise 7: Check if Item Exists
Task:
Check if "apple" is in the list.
Solution:
fruits = ["banana", "apple", "mango"]
if "apple" in fruits:
print("Found")
else:
print("Not Found")
Output:
Found
🔹 Exercise 8: List Length
Task:
Find the number of items in a list.
Solution:
numbers = [10, 20, 30, 40]
print(len(numbers))
Output:
4
🔹 Exercise 9: Sort a List
Task:
Sort numbers in ascending order.
Solution:
numbers = [5, 2, 9, 1, 7]
numbers.sort()
print(numbers)
Output:
[1, 2, 5, 7, 9]
🔹 Exercise 10: Reverse a List
Task:
Reverse the list order.
Solution:
numbers = [1, 2, 3, 4]
numbers.reverse()
print(numbers)
Output:
[4, 3, 2, 1]
🔹 Exercise 11: Copy a List
Task:
Create a copy of a list and modify it.
Solution:
list1 = [1, 2, 3]
list2 = list1.copy()
list2.append(4)
print(list1)
print(list2)
Output:
[1, 2, 3]
[1, 2, 3, 4]
🔹 Exercise 12: Join Two Lists
Task:
Merge two lists into one.
Solution:
list1 = [1, 2, 3]
list2 = [4, 5, 6]
result = list1 + list2
print(result)
Output:
[1, 2, 3, 4, 5, 6]
🔹 Exercise 13: Find Maximum and Minimum
Task:
Find largest and smallest number.
Solution:
numbers = [10, 5, 8, 20, 3]
print(max(numbers))
print(min(numbers))
Output:
20
3
🔹 Exercise 14: Count Occurrences
Task:
Count how many times a value appears.
Solution:
numbers = [1, 2, 2, 3, 2, 4]
print(numbers.count(2))
Output:
3
🔹 Exercise 15: Square Numbers Using List Comprehension
Task:
Create a list of squares.
Solution:
squares = [x * x for x in range(1, 6)]
print(squares)
Output:
[1, 4, 9, 16, 25]
🔹 Bonus Challenge (Intermediate)
Task:
Remove all even numbers from a list.
Solution:
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
result = [x for x in numbers if x % 2 != 0]
print(result)
Output:
[1, 3, 5, 7]
🚀 Conclusion
Practicing Python list exercises helps you:
- Improve logic building
- Understand list operations deeply
- Prepare for real-world coding tasks
Lists are one of the most important data structures in Python, and mastering them will make you a strong Python programmer.


0 Comments