Python List Exercises
Beginner
1. Create a list of 5 fruits. Print the 2nd and last item using indexing.
2. Given
nums = [4, 8, 15, 16, 23, 42]
find the sum and average without using sum().
3. Reverse a list without using .reverse() or slicing ([::-1]).
4. Given
a = [1, 2, 3]
add 4 and 5 to it using two different methods (.append() vs .extend()), and explain the
difference in output.
Intermediate
5. Given
marks = [56, 78, 90, 45, 88, 67]
write code to:
• Count how many students scored above 60
• Find the highest and lowest scores without using max()/min()
6. Remove all duplicate values from
data = [1, 3, 3, 5, 5, 5, 7, 9, 9]
while preserving the original order.
7. Given two lists
a = [1, 2, 3]
b = [4, 5, 6]
combine them into a list of tuples: [(1,4), (2,5), (3,6)] — first do it manually with
a loop, then again using zip().
8. Write a function flatten(nested list) that converts [[1,2], [3,4], [5,6]] into [1,2,3,4,5,6].
1
Advanced
9. Given a list of transaction amounts
txns = [200, -50, 300, -120, 75, -30]
separate them into two lists: credits and debits, using list comprehension.
10. Given
prices = [10, 20, 15, 30, 25]
find the maximum profit possible if you buy on one day and sell on a later day (classic
“buy low sell high” problem).
11. Implement your own version of sorted() for a list of numbers using bubble sort (don’t
use Python’s built-in sort).
12. Given a list of dictionaries:
students = [
{"name": "Ravi", "marks": 78},
{"name": "Aisha", "marks": 92},
{"name": "Tom", "marks": 65}
]
Sort this list by marks in descending order without using sorted(key=...) — write
the logic manually. Then redo it using sorted() with key and lambda to see how much
shorter it is.