List Exercises in Python

Test your understanding of Python lists with these exercises. Each exercise is designed to help you practice different list operations and concepts.

Exercise 1: Create a List

Create a list of your three favorite fruits and print it.

Python
# Your code here
fruits = ["apple", "banana", "cherry"]
print(fruits)

Exercise 2: Access a List Item

Access the second item in the list fruits and print it.

Python
# Your code here
print(fruits[1])  # Output should be "banana"

Exercise 3: Modify a List Item

Change the first item in the list to "blueberry" and print the list.

Python
# Your code here
fruits[0] = "blueberry"
print(fruits)

Exercise 4: Add a List Item

Add "date" to the end of the list and print the updated list.

Python
# Your code here
fruits.append("date")
print(fruits)

Exercise 5: Remove a List Item

Remove "banana" from the list and print the updated list.

Python
# Your code here
fruits.remove("banana")
print(fruits)

Exercise 6: Insert an Item at a Specific Position

Insert "orange" at the second position in the list and print the updated list.

Python
# Your code here
fruits.insert(1, "orange")
print(fruits)

Exercise 7: Find the Length of the List

Find and print the number of items in the list fruits.

Python
# Your code here
print(len(fruits))

Exercise 8: Sort the List

Sort the list fruits in alphabetical order and print it.

Python
# Your code here
fruits.sort()
print(fruits)

Exercise 9: Reverse the List

Reverse the order of the list fruits and print the updated list.

Python
# Your code here
fruits.reverse()
print(fruits)

Exercise 10: Clear the List

Remove all items from the list fruits and print the empty list.

Python
# Your code here
fruits.clear()
print(fruits)  # Output should be an empty list: []