Explore programming tutorials, exercises, quizzes, and solutions!
Python Lists Exercises
1/20
You're starting to learn about lists in Python — one of the most commonly used data structures for storing sequences of items. Python lists are flexible, ordered, and mutable, meaning they can hold multiple values and can be changed after creation.
What does this example demonstrate about Python lists?
This example demonstrates that Python lists are ordered and mutable — meaning their contents can change over time.
Here, fruits.append("mango") adds "mango" to the end of the existing list. This change is permanent and affects the original list.
After the code runs, the output will be:
['apple', 'banana', 'cherry', 'mango']
The append() method is commonly used to add new elements, and print() can display list contents directly.
Python lists are dynamic — they grow and shrink as needed and can hold different data types, though it’s most common to use them with related items like this.
Which method is used to add an element to the end of a list?
The append() method adds a single element to the end of the list. For example, my_list.append(5) adds 5 to the list.
The insert() method adds an element at a specific index and shifts the remaining elements to the right. In this case, 100 is inserted at index 2. The original value at index 2 (88) and the rest of the list move one position to the right.
What does the following code output?
data = [5, 3, 9, 1]
data.sort()
print(data)
The sort() method arranges the elements of the list in ascending order. It modifies the list in-place. So, the list becomes [1, 3, 5, 9] after sorting numerically.
Which of the following correctly removes the second element from the list below?
items = ['pen', 'notebook', 'eraser', 'ruler']
del items[1] deletes the element at index 1, which is the second element ('notebook'). Option 1 would try to remove the value 1, not the element at index 1. Option 3 removes the third element, and option 4 removes the last one.
What will this code print?
a = [1, 2, 3]
b = a
b.append(4)
print(a)
Both a and b point to the same list in memory. When you modify b, you're also modifying a. Therefore, a is updated and becomes [1, 2, 3, 4].
Which method would you use to combine two lists into one?
The extend() method adds each element of another list to the end of the current list. For example, list1.extend(list2) will add all elements of list2 into list1 individually, not as a sublist.
What will the following code output?
lst = [1, 2, 3, 4, 5, 6]
print(lst[::2])
The lst[::2] uses slicing to extract elements starting from index 0, with a step of 2. This means every second element from the list will be selected, resulting in [1, 3, 5].
In this case, we are modifying the slice of the list starting from index 1 up to index 4 (not including index 4). The values at indices 1, 2, and 3 (20, 30, and 40) are replaced by [60, 70], resulting in the list [10, 60, 70, 40, 50].
What will the following code output?
lst = [1, 2, 3, 4, 5]
lst.remove(6)
print(lst)
The remove() method removes the first occurrence of the specified element. If the element doesn't exist in the list (as in this case, where 6 is not in the list), it raises a ValueError.
What will be the output of the following code?
lst = [1, 2, 3, 4, 5]
lst.reverse()
print(lst)
The reverse() method reverses the elements of the list in place. After calling lst.reverse(), the list becomes [5, 4, 3, 2, 1].
What will the following code output?
a = [1, 2, 3]
b = [4, 5, 6]
a.append(b)
print(a)
The append() method adds the entire list b as a single element to list a. Therefore, the result will be [1, 2, 3, [4, 5, 6]], with the list [4, 5, 6] being nested inside a.
In this case, the slice lst[1:3] replaces the elements at indices 1 and 2 (values 2 and 3) with the new list [6, 7, 8]. Therefore, the result is [1, 6, 7, 8, 5], with the element at index 3 (4) remaining unchanged.
The remove() method removes the first occurrence of the specified element. When you try to remove 6 the second time, it raises a ValueError because 6 no longer exists in the list.
What will be the output of this code?
lst = [1, 2, 3, 4, 5]
lst = lst[::-1]
print(lst)
The [::-1] slice notation reverses the list. This operation creates a new list, so lst is now [5, 4, 3, 2, 1], which is the reverse of the original list.
When append() is used, it adds the entire list [6, 7] as a single element to lst, making the list [1, 2, 3, 4, 5, [6, 7]]. This increases the length of the list to 7.
The slice lst[1:3] replaces the elements at indices 1 and 2 (values 2 and 3) with the new list [10, 11, 12]. This results in the list [1, 10, 11, 12, 3, 4, 5], with the elements at index 3 (4) and 4 (5) remaining unchanged.
Practicing Python Lists? Don’t forget to test yourself later in
our
Python Quiz.
About This Exercise: Python – Lists
Welcome to the Python Lists exercises, a thoughtfully crafted set of programming challenges designed to help you master one of Python’s most versatile and widely used data structures — Lists. Whether you’re a complete beginner just starting out with Python or an experienced developer aiming to deepen your understanding of Python Lists, this exercise collection will guide you through fundamental concepts and practical applications step-by-step.
Lists in Python are essential for storing ordered collections of items, making them invaluable in almost every programming scenario — from managing datasets to implementing algorithms. These exercises will take you through a comprehensive journey covering key Python List operations such as indexing, slicing, appending, removing, and sorting elements. You will also encounter more advanced topics like nested Lists, List comprehensions, and the performance considerations involved when working with large Lists.
By working through these Python Lists exercises, you will not only learn how to manipulate Lists effectively but also develop problem-solving skills crucial for tackling real-world programming challenges. Each exercise is designed with practical scenarios in mind to prepare you for coding interviews, academic assessments, and professional Python development. The gradual increase in difficulty ensures that you build confidence as you progress from simple to complex List operations.
In addition to the Lists-focused problems, we encourage you to explore other related Python topics such as dictionaries, tuples, sets, and control flow structures, all available on our platform. These interconnected concepts form the backbone of Python programming, and mastering them will elevate your coding proficiency. Don’t forget to complement your exercise practice with our interactive Python quizzes, which are crafted to reinforce your understanding and highlight areas where you can improve further.
Consistent practice is the key to becoming a proficient Python developer, and focusing on Lists is an excellent way to strengthen your coding fundamentals. As you solve these exercises, you will gain insight into Python’s flexibility and power, allowing you to write cleaner, more efficient code. Whether you plan to build web applications, analyze data, or develop software tools, a strong grasp of Lists will serve as a solid foundation for your programming journey.
Start exploring the Python Lists exercises now to unlock your potential and accelerate your learning. With dedication and practice, you’ll soon be able to confidently handle complex data manipulation tasks and excel in any Python programming challenge that comes your way.