Python Lists: Exercises

Having mastered the basics of lists in Python, it’s time to put your skills to the test. Try solving the following exercises to solidify your understanding:

Exercise 1: Accessing List Items

Print the second item from the list of books:

books = ["1984", "The Great Gatsby", "To Kill a Mockingbird"]
print(books[1])  # Expected output: "The Great Gatsby"

Exercise 2: Modifying List Items

Change the value of the first item in the list to “Brave New World”:

books = ["1984", "The Great Gatsby", "To Kill a Mockingbird"]
books[0] = "Brave New World"
print(books)  # Expected output: ['Brave New World', 'The Great Gatsby', 'To Kill a Mockingbird']

Exercise 3: Adding Items

Add “The Catcher in the Rye” to the end of the list:

books = ["1984", "The Great Gatsby", "To Kill a Mockingbird"]
books.append("The Catcher in the Rye")
print(books)  # Expected output: ['1984', 'The Great Gatsby', 'To Kill a Mockingbird', 'The Catcher in the Rye']

Exercise 4: Removing Items

Remove “The Great Gatsby” from the list:

books = ["1984", "The Great Gatsby", "To Kill a Mockingbird"]
books.remove("The Great Gatsby")
print(books)  # Expected output: ['1984', 'To Kill a Mockingbird']

Exercise 5: Inserting Items

Insert “Brave New World” at the second position:

books = ["1984", "The Great Gatsby", "To Kill a Mockingbird"]
books.insert(1, "Brave New World")
print(books)  # Expected output: ['1984', 'Brave New World', 'The Great Gatsby', 'To Kill a Mockingbird']

Exercise 6: List Concatenation

Combine these two lists into one:

list1 = ["1984", "The Great Gatsby"]
list2 = ["To Kill a Mockingbird", "Brave New World"]
list3 = list1 + list2
print(list3)  # Expected output: ['1984', 'The Great Gatsby', 'To Kill a Mockingbird', 'Brave New World']

Exercise 7: Copying a List

Create a copy of the list and modify it:

original = ["1984", "The Great Gatsby", "To Kill a Mockingbird"]
copied = original.copy()
copied.append("The Catcher in the Rye")
print(copied)  # Expected output: ['1984', 'The Great Gatsby', 'To Kill a Mockingbird', 'The Catcher in the Rye']

Exercise 8: Sorting a List

Sort the list in ascending order:

numbers = [25, 42, 18, 7, 31]
numbers.sort()
print(numbers)  # Expected output: [7, 18, 25, 31, 42]

Exercise 9: Reversing a List

Reverse the order of items in the list:

numbers = [1, 2, 3, 4, 5]
numbers.reverse()
print(numbers)  # Expected output: [5, 4, 3, 2, 1]

Exercise 10: Counting Elements

Count how many times “1984” appears in the list:

books = ["1984", "The Great Gatsby", "1984", "To Kill a Mockingbird"]
count = books.count("1984")
print(count)  # Expected output: 2

Exercise 11: Finding an Index

Find the index of “To Kill a Mockingbird” in the list:

books = ["1984", "The Great Gatsby", "To Kill a Mockingbird"]
index = books.index("To Kill a Mockingbird")
print(index)  # Expected output: 2

Exercise 12: Extending a List

Extend the list with more books:

books = ["1984", "The Great Gatsby"]
more_books = ["To Kill a Mockingbird", "Brave New World"]
books.extend(more_books)
print(books)  # Expected output: ['1984', 'The Great Gatsby', 'To Kill a Mockingbird', 'Brave New World']

Exercise 13: Popping Items

Remove the last item from the list and print it:

books = ["1984", "The Great Gatsby", "To Kill a Mockingbird"]
last_item = books.pop()
print(last_item)  # Expected output: 'To Kill a Mockingbird'

Exercise 14: Removing by Value

Remove “1984” from the list:

books = ["1984", "The Great Gatsby", "To Kill a Mockingbird"]
books.remove("1984")
print(books)  # Expected output: ['The Great Gatsby', 'To Kill a Mockingbird']

Exercise 15: List Comprehension

Create a list of the lengths of book titles:

book_lengths = [len(book) for book in ["1984", "The Great Gatsby", "To Kill a Mockingbird"]]
print(book_lengths)  # Expected output: [4, 16, 21]

Exercise 16: Slicing a List

Extract a sublist from the second to the fourth item:

books = ["1984", "The Great Gatsby", "To Kill a Mockingbird", "Brave New World", "The Catcher in the Rye"]
sublist = books[1:4]
print(sublist)  # Expected output: ['The Great Gatsby', 'To Kill a Mockingbird', 'Brave New World']

Exercise 17: Checking for Existence

Check if “Brave New World” is in the list:

books = ["1984", "The Great Gatsby", "To Kill a Mockingbird", "Brave New World"]
exists = "Brave New World" in books
print(exists)  # Expected output: True

Exercise 18: Using While Loop

Print all items in the list using a while loop:

books = ["1984", "The Great Gatsby", "To Kill a Mockingbird"]
i = 0
while i < len(books):
    print(books[i])
    i += 1
# Expected output:
# 1984
# The Great Gatsby
# To Kill a Mockingbird

Exercise 19: Using For Loop

Print all items in the list using a for loop:

books = ["1984", "The Great Gatsby", "To Kill a Mockingbird"]
for book in books:
    print(book)
# Expected output:
# 1984
# The Great Gatsby
# To Kill a Mockingbird

Exercise 20: Using List Methods

Apply various list methods to manipulate the list:

books = ["1984", "The Great Gatsby", "To Kill a Mockingbird"]
books.append("Brave New World")
books.remove("The Great Gatsby")
books.sort()
print(books)  # Expected output: ['1984', 'Brave New World', 'To Kill a Mockingbird']

These exercises will help you practice and master different list operations in Python. Work through each challenge to enhance your skills!

Leave a Comment

Voice control in the smart home : what devices work best with alexa and google assistant ?. Royal medium upholstered 3 panel kist quantity.