List Exercises

These exercises are designed to strengthen your understanding of Python lists step by step. They cover accessing, modifying, looping, methods, logic, and tricky behaviors.

Exercise 1: Create and Print a List

Create a list of five integers and print it.
python
numbers = [10, 20, 30, 40, 50]
print(numbers)

Exercise 2: Access List Items

Print:
  • First item
  • Last item
  • Third item
python
numbers = [10, 20, 30, 40, 50]

print(numbers[0])
print(numbers[-1])
print(numbers[2])

Exercise 3: Change List Items

Change the second element of the list to 99.
python
numbers = [10, 20, 30, 40]
numbers[1] = 99
print(numbers)

Exercise 4: Add Items to a List

Add 60 at the end and 5 at the beginning.
python
numbers = [10, 20, 30]
numbers.append(60)
numbers.insert(0, 5)
print(numbers)

Exercise 5: Remove Items from a List

Remove:
  • Value 20
  • Last item
python
numbers = [10, 20, 30, 40]
numbers.remove(20)
numbers.pop()
print(numbers)

Exercise 6: Loop Through a List

Print each element using a for loop.
python
colors = ["red", "green", "blue"]

for color in colors:
    print(color)

Exercise 7: Loop Using Index

Print index and value.
python
colors = ["red", "green", "blue"]

for i in range(len(colors)):
    print(i, colors[i])

Exercise 8: Count Occurrences

Count how many times 2 appears.
python
numbers = [1, 2, 3, 2, 4, 2]
print(numbers.count(2))

Exercise 9: Sort a List

Sort the list in ascending and descending order.
python
numbers = [5, 1, 4, 2, 3]
numbers.sort()
print(numbers)

numbers.sort(reverse=True)
print(numbers)

Exercise 10: Copy a List Correctly

Create a copy and modify only the copied list.
python
a = [1, 2, 3]
b = a.copy()
b.append(4)

print(a)
print(b)

Exercise 11: Join Two Lists

Join two lists into one.
python
a = [1, 2]
b = [3, 4]
result = a + b
print(result)

Exercise 12: List Slicing

Print:
  • First three items
  • Last two items
python
numbers = [10, 20, 30, 40, 50]

print(numbers[:3])
print(numbers[-2:])

Exercise 13: Remove Even Numbers (Logic)

Remove all even numbers from the list.
python
numbers = [1, 2, 3, 4, 5, 6]
numbers = [x for x in numbers if x % 2 != 0]
print(numbers)

Exercise 14: Find Largest and Smallest Element

python
numbers = [10, 5, 20, 3, 15]

print(max(numbers))
print(min(numbers))

Exercise 15: Reverse a List

python
numbers = [1, 2, 3, 4]
numbers.reverse()
print(numbers)

Exercise 16: Nested List Access

Print the value 5.
python
matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

print(matrix[1][1])

Exercise 17: Flatten a Nested List

python
matrix = [[1, 2], [3, 4], [5, 6]]
flat = [num for row in matrix for num in row]
print(flat)

Exercise 18: Check Item Exists

Check if "apple" exists.
python
fruits = ["apple", "banana", "cherry"]

if "apple" in fruits:
    print("Apple found")

Exercise 19: List Length Without len()

python
numbers = [10, 20, 30]
count = 0

for _ in numbers:
    count += 1

print(count)

Exercise 20: Tricky Reference Question

Predict the output.
python
a = [1, 2]
b = a
b.append(3)

print(a)
print(b)

Key Learning Outcomes

  • List indexing and slicing
  • Adding and removing items
  • Looping techniques
  • List methods usage
  • Copy vs reference
  • Logical list manipulation
These exercises cover real interview-level understanding of Python lists.