Python list is the most widely used data structure, and a good understanding of it is necessary. This Python list exercise aims to help developers learn and practice list operations. All questions are tested on Python 3.
This Python list exercise includes the following: –
The exercise contains 10 questions and solutions provided for each question. You need to solve and practice different list programs, questions, problems, and challenges.
Questions cover the following list topics:
- list operations and manipulations
- list functions
- list slicing
- list comprehension
When you complete each question, you get more familiar with the Python list type. Let us know if you have any alternative solutions in the comment section below.
- Use Online Code Editor to solve exercise questions.
- Read the Complete guide on Python List to solve this exercise.
Table of contents
- Exercise 1: Reverse a list in Python
- Exercise 2: Concatenate two lists index-wise
- Exercise 3: Turn every item of a list into its square
- Exercise 4: Concatenate two lists in the following order
- Exercise 5: Iterate both lists simultaneously
- Exercise 6: Remove empty strings from the list of strings
- Exercise 7: Add new item to list after a specified item
- Exercise 8: Extend nested list by adding the sublist
- Exercise 9: Replace list’s item with new value if found
- Exercise 10: Remove all occurrences of a specific item from a list.
Exercise 1: Reverse a list in Python
Given:
list1 = [100, 200, 300, 400, 500]
Code language: Python (python)
Expected output:
[500, 400, 300, 200, 100]
Show Hint
Use the list function reverse()
Show Solution
Solution 1: list function reverse()
Solution 2: Using negative slicing
-1 indicates to start from the last item.
Exercise 2: Concatenate two lists index-wise
Write a program to add two lists index-wise. Create a new list that contains the 0th index item from both the list, then the 1st index item, and so on till the last element. any leftover items will get added at the end of the new list.
Given:
list1 = ["M", "na", "i", "Ke"]
list2 = ["y", "me", "s", "lly"]
Code language: Python (python)
Expected output:
['My', 'name', 'is', 'Kelly']
Show Hint
Use list comprehension with the zip()
function
Show Solution
Use the zip()
function. This function takes two or more iterables (like list, dict, string), aggregates them in a tuple, and returns it.
Exercise 3: Turn every item of a list into its square
Given a list of numbers. write a program to turn every item of a list into its square.
Given:
numbers = [1, 2, 3, 4, 5, 6, 7]
Code language: Python (python)
Expected output:
[1, 4, 9, 16, 25, 36, 49]
Show Hint
Iterate numbers from a list one by one using a for loop and calculate the square of the current number
Show Solution
Solution 1: Using loop and list method
- Create an empty result list
- Iterate a numbers list using a loop
- In each iteration, calculate the square of a current number and add it to the result list using the
append()
method.
Solution 2: Use list comprehension
Exercise 4: Concatenate two lists in the following order
list1 = ["Hello ", "take "]
list2 = ["Dear", "Sir"]
Code language: Python (python)
Expected output:
['Hello Dear', 'Hello Sir', 'take Dear', 'take Sir']
Show Hint
Use a list comprehension to iterate two lists using a for loop and concatenate the current item of each list.
Show Solution
Exercise 5: Iterate both lists simultaneously
Given a two Python list. Write a program to iterate both lists simultaneously and display items from list1 in original order and items from list2 in reverse order.
Given
list1 = [10, 20, 30, 40]
list2 = [100, 200, 300, 400]
Code language: Python (python)
Expected output:
10 400 20 300 30 200 40 100
Show Solution
- The
zip()
function can take two or more lists, aggregate them in a tuple, and returns it. - Pass the first argument as a
list1
and seconds argument as alist2[::-1]
(reverse list using list slicing) - Iterate the result using a
for
loop
Exercise 6: Remove empty strings from the list of strings
list1 = ["Mike", "", "Emma", "Kelly", "", "Brad"]
Code language: Python (python)
Expected output:
["Mike", "Emma", "Kelly", "Brad"]
Show Hint
Use a filter()
function to remove the None
/ empty type from the list
Show Solution
Use a filter()
function to remove None
type from the list
Exercise 7: Add new item to list after a specified item
Write a program to add item 7000 after 6000 in the following Python List
Given:
list1 = [10, 20, [300, 400, [5000, 6000], 500], 30, 40]
Code language: Python (python)
Expected output:
[10, 20, [300, 400, [5000, 6000, 7000], 500], 30, 40]
Show Hint
The given list is a nested list. Use indexing to locate the specified item, then use the append()
method to add a new item after it.
Show Solution
Use the append()
method
Exercise 8: Extend nested list by adding the sublist
You have given a nested list. Write a program to extend it by adding the sublist ["h", "i", "j"]
in such a way that it will look like the following list.
Given List:
list1 = ["a", "b", ["c", ["d", "e", ["f", "g"], "k"], "l"], "m", "n"]
# sub list to add
sub_list = ["h", "i", "j"]
Code language: Python (python)
Expected Output:
['a', 'b', ['c', ['d', 'e', ['f', 'g', 'h', 'i', 'j'], 'k'], 'l'], 'm', 'n']
Show Hint
The given list is a nested list. Use indexing to locate the specified sublist item, then use the extend()
method to add new items after it.
Show Solution
Exercise 9: Replace list’s item with new value if found
You have given a Python list. Write a program to find value 20 in the list, and if it is present, replace it with 200. Only update the first occurrence of an item.
Given:
list1 = [5, 10, 15, 20, 25, 50, 20]
Code language: Python (python)
Expected output:
[5, 10, 15, 200, 25, 50, 20]
Show Hint
- Use list method
index(20)
to get the index number of a 20 - Next, update the item present at the location using the index number
Show Solution
Exercise 10: Remove all occurrences of a specific item from a list.
Given a Python list, write a program to remove all occurrences of item 20.
Given:
list1 = [5, 20, 15, 20, 25, 50, 20]
Code language: Python (python)
Expected output:
[5, 15, 25, 50]
Show Solution
Solution 1: Use the list comprehension
Solution 2: while loop (slow solution)