Python list is the most widely used data structure, and a good understanding of list operations is necessary. This Python list exercise aims to help developers learn and practice list operations to improve their knowledge of the Python list. All questions are tested on Python 3.
Also Read:
This Python list exercise includes the following: –
The exercise contains 10 questions and solutions provided for each question. In this Python List assignment, you need to solve and practice different list programs, questions, problems, and challenges.
It covers questions on the following list topics:
- Python List operations and manipulations
- Python List functions
- Python list slicing
- Python list comprehension
When you complete each question, you get more familiar with the Python list. Let us know if you have any alternative solutions. It will help other developers.
Use Online Code Editor to solve exercise questions.
Read: Complete Guide on Python List
Exercise 1: Reverse a given list in Python
aLsit = [100, 200, 300, 400, 500]
Expected output:
[500, 400, 300, 200, 100]
Show Solution
aList = [100, 200, 300, 400, 500]
aList = aList[::-1]
print(aList)
Exercise 2: Concatenate two lists index-wise
Given:
list1 = ["M", "na", "i", "Ke"]
list2 = ["y", "me", "s", "lly"]
Expected output:
['My', 'name', 'is', 'Kelly']
Show Solution
list1 = ["M", "na", "i", "Ke"]
list2 = ["y", "me", "s", "lly"]
list3 = [i + j for i, j in zip(list1, list2)]
print(list3)
Exercise 3: Given a Python list of numbers. Turn every item of a list into its square
Given:
aList = [1, 2, 3, 4, 5, 6, 7]
Expected output:
[1, 4, 9, 16, 25, 36, 49]
Show Solution
aList = [1, 2, 3, 4, 5, 6, 7]
aList = [x * x for x in aList]
print(aList)
Exercise 4: Concatenate two lists in the following order
list1 = ["Hello ", "take "]
list2 = ["Dear", "Sir"]
Expected output:
['Hello Dear', 'Hello Sir', 'take Dear', 'take Sir']
Show Solution
list1 = ["Hello ", "take "]
list2 = ["Dear", "Sir"]
resList = [x+y for x in list1 for y in list2]
print(resList)
Exercise 5: Given a two Python list. Iterate both lists simultaneously such that list1 should display item in original order and list2 in reverse order
Given
list1 = [10, 20, 30, 40]
list2 = [100, 200, 300, 400]
Expected output:
10 400 20 300 30 200 40 100
Show Solution
We need to use the zip()
function here.
list1 = [10, 20, 30, 40]
list2 = [100, 200, 300, 400]
for x, y in zip(list1, list2[::-1]):
print(x, y)
Exercise 6: Remove empty strings from the list of strings
list1 = ["Mike", "", "Emma", "Kelly", "", "Brad"]
Expected output:
["Mike", "Emma", "Kelly", "Brad"]
Show Solution
Use a filter()
function to remove None
type from the list
list1 = ["Mike", "", "Emma", "Kelly", "", "Brad"]
resList = list(filter(None, list1))
print(resList)
Exercise 7: Add item 7000 after 6000 in the following Python List
Given:
list1 = [10, 20, [300, 400, [5000, 6000], 500], 30, 40]
Expected output:
[10, 20, [300, 400, [5000, 6000, 7000], 500], 30, 40]
Show Solution
Use the append()
method
list1 = [10, 20, [300, 400, [5000, 6000], 500], 30, 40]
list1[2][2].append(7000)
print(list1)
Exercise 8: Given a nested list extend it by adding the sub list ["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 be added = ["h", "i", "j"]
Expected output:
['a', 'b', ['c', ['d', 'e', ['f', 'g', 'h', 'i', 'j'], 'k'], 'l'], 'm', 'n']
Show Solution
list1 = ["a", "b", ["c", ["d", "e", ["f", "g"], "k"], "l"], "m", "n"]
subList = ["h", "i", "j"]
list1[2][1][2].extend(subList)
print(list1)
Exercise 9: Given a Python list, find value 20 in the list, and if it is present, replace it with 200. Only update the first occurrence of a value
Given
list1 = [5, 10, 15, 20, 25, 50, 20]
Expected output:
list1 = [5, 10, 15, 200, 25, 50, 20]
Show Solution
list1 = [5, 10, 15, 20, 25, 50, 20]
index = list1.index(20)
list1[index] = 200
print(list1)
Exercise 10: Given a Python list, remove all occurrence of 20 from the list
list1 = [5, 20, 15, 20, 25, 50, 20]
Expected output:
[5, 15, 25, 50]
Show Solution
Use the list comprehension
list1 = [5, 20, 15, 20, 25, 50, 20]
def removeValue(sampleList, val):
return [value for value in sampleList if value != val]
resList = removeValue(list1, 20)
print(resList)
Exercise 10: Given a Python list, remove all occurrence of 20 from the list
Result: [5, 15, 25, 50]
please tell how we decide dimension as in ques 8 list[2][1][2]
Problem 1:
Other Approach:- The Below also works
Another solution for Q6
Hi all! this code should remove all negative values from the given list and print a new list with only positive values for some reason it prints an iteration of the given list before the positive list. can I get any hints on how to solve that, please?
Alternative answer for Q10:
Alternative answer for Q10:
One another:
Here’s another
Can anyone please point out the mistake, or is this code correct…..
you must loop over the remove function in order to work
your program is correct.it is running without any error
[5,10,15,300,25,50,20]
Any alternative ans for Q-2
question no.10
Thanks..it really helps:-)
Questions 4:
Any alternate answers for Q-10
Here is an alternate solution for question no.10
Alternative answer to Q9 — Note: would have convert back to a list to satisfy the question requirements.
Thanks Vishal, I am a beginner level python learner and I am doing the exercises , I found these helpful because these cover many methods of data structure and challenges as well. I enjoy solving them.
You’re welcome, Qadeer Rizvi.
sir your article are amazing very helpful for me.keep it up and live long
Thank you, Bostan Khan, for your kind words
list1[2][1][2].extend(subList)
i can’t understand the indexing value.please heip me out.
I also had a problem with that. I printed all values in order to understand and it helped me.
Try first print:
list1[0]
then look at
list1[1]
then try
list1[0][0]
then
list1[1][0]
etc.
If you will see it then it will be easier to understand indexing.
can you explain Q4 flow of code not able to understand
its work like nested loop,which mean for each iteration of outer loop the inner loop will be executed completely.keep in mind nested loop and execute it then you guess easily
Good Work Vishal !! Do you run any online classes for Python. I am ready to join in your class
Hey Prashanth, I am not running any classes as of now. I will definitely let you know once I started it.
Hi team,
I’m having trouble understanding the specific append questions where you have to place an item inside a specific list (Q7, Q8). Is there a trick people use to figure out how the indexing associated with the lists work? Playing around, it seems most the index changes result in an error.
Thanks!
Question 7
Question 8
I don’t understand the index of lists. Where they come from? Does someone want to explain?
its like array indexing access ,the point here to be noted each list in the list will be consider an element of the list
Hey Marcin
Just count values in first index, then in second, until you reach the destination index to apply append/extend function.
lets say in Q7.
there are two values in first index before second index initiate so use 2 for that, then again we have 2 values in second index before 3rd and destination index start so use 2 again. Now you can use append function here which is third index.
for question1, can we use the below code?
which gives the same output as expected, maybe a lengthy code but just want to know whether we can use this method
Thanks in advance
The idea is to use the existing data and you need to supply method. If the data change, for example is has no uniform step interval, then your code fails.
question 5: i think this method is more easy and if this method is not preferred then pls reply
Hi, great collection of exercises. For question 7 is there any way to run a loop and extract all the elements while ignoring the sub-lists ?
Hey Noma, Please use the following code
Thanks sir it gave a lot of help to me. Thanks again
Thank you Vishal – This is by far the best website on python for practise,
request to share anylinks or references to read furtheron python topics for begineers
Thank you, Pallavi. Within two weeks you will see new topics for beginners on PYnative