PYnative

Python Programming

  • Learn Python
  • Exercises
  • Quizzes
  • Code Editor
  • Tricks
Home » Python Exercises » Python List Exercise with Solutions

Python List Exercise with Solutions

Updated on: March 9, 2021 | Python Tags: Basics Python Python Exercises

TweetF  sharein  shareP  Pin

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:

  • Python List
  • Python List Quiz

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)

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

Founder of PYnative.com I am a Python developer and I love to write articles to help developers. Follow me on Twitter. All the best for your future Python endeavors!

Related Tutorial Topics:

Basics Python Python Exercises

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 10 questions
  • Each Quiz contains 12-15 MCQ
Exercises
Quizzes

Leave a Reply Cancel reply

your email address will NOT be published. all comments are moderated according to our comment policy.

Use <pre> tag for posting code. E.g. <pre> Your code </pre>

52 Comments

Comments

  1. Siddhartha Roy says

    April 7, 2021 at 4:55 am

    Exercise 10: Given a Python list, remove all occurrence of 20 from the list

    list1 = [5, 20, 15, 20, 25, 50, 20]
    for i in list1:
        if i == 20:
            list1.remove(20)
    print(list1)

    Result: [5, 15, 25, 50]

    Reply
  2. rocky says

    April 1, 2021 at 3:45 pm

    def removeValue(sampleList, val):
       return [value for value in sampleList if value != val]
    
    resList = removeValue(list1, 20)
    print(resList)
    Reply
  3. Aakanksha Jagyasi says

    December 25, 2020 at 6:30 pm

    please tell how we decide dimension as in ques 8 list[2][1][2]

    Reply
  4. Kumar says

    November 25, 2020 at 6:03 pm

    Problem 1:

    aList = [100, 200, 300, 400, 500]
    aList = aList[::-1]
    print(aList)

    Other Approach:- The Below also works

    aList = [100, 200, 300, 400, 500]
    aLsit.reverse()
    print(aList)
    Reply
  5. Arpit says

    November 13, 2020 at 12:01 pm

    Another solution for Q6

    list1 = [“Mike”, “”, “Emma”, “Kelly”, “”, “Brad”]
    print([item for item in list1 if len(item)>0] )
    Reply
  6. Mohammed Hisham Mohammed says

    October 27, 2020 at 8:55 pm

    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?

    def removeNegatives(listOfIntegers):
    
        for item in listOfIntegers:
            if item < 0: 
                listOfIntegers[listOfIntegers.index(item)] = -item
    
        print (listOfIntegers)
    Reply
  7. isMeStranger says

    October 15, 2020 at 4:28 pm

    Alternative answer for Q10:

     list1 = [5, 20, 15, 20, 25, 50, 20]
               for x in list1:
                      if x == 20:
                           list1.remove(20)
               print(list1)
    Reply
  8. isMeStranger says

    October 15, 2020 at 4:27 pm

    Alternative answer for Q10:

    list1 = [5, 20, 15, 20, 25, 50, 20]
    for x in list1:
      if x == 20:
        list1.remove(20)
    print(list1)
    Reply
    • Paweł says

      November 12, 2020 at 12:44 am

      One another:

      list1 = [5, 20, 15, 20, 25, 50, 20]
      z = list(filter(lambda x: x!=20, list1))
      print(z)
      Reply
    • Kita says

      November 24, 2020 at 8:40 am

      Here’s another

      
       list1 = [5, 10, 15, 20, 25, 50, 20]
       while 20 in list1:
           list1.remove(20)
       print(list1)
      
      Reply
  9. bhavya says

    August 21, 2020 at 11:01 am

    Can anyone please point out the mistake, or is this code correct…..

    list1 = [5, 10, 15, 20, 25, 50, 20]
    if 20 in list1:
         list1.remove(20)
         list1.insert(3, 200)
    
    print(list1)
    Reply
    • mohmmad says

      September 5, 2020 at 6:09 pm

      you must loop over the remove function in order to work

      Reply
    • mohmmad says

      September 5, 2020 at 6:11 pm

      list1 = [5, 10, 15, 20, 25, 50, 20]
      for x in list1:
          if 20 in list1:
              list1.remove(20)
      list1.insert(3,200)
      print(list1)
      Reply
    • Charan says

      October 1, 2020 at 7:57 pm

      your program is correct.it is running without any error

      Reply
    • Adish Jain says

      October 4, 2020 at 4:05 pm

      [5,10,15,300,25,50,20]

      Reply
  10. sheena says

    August 18, 2020 at 1:41 pm

    list12 = [5, 20, 15, 20, 25, 50, 20]
    for i in list12:
        if(i==20):
            list12.remove(i)
    print(list12)
    
    		
    Reply
  11. Deepak Sen says

    August 12, 2020 at 11:24 am

    Any alternative ans for Q-2

    Reply
    • sheena says

      August 18, 2020 at 1:43 pm

      list1 = ["M", "na", "i", "Ke"]
      list2 = ["y", "me", "s", "lly"]
      newlist=list((zip(list1, list2)))
      aa=[]
      for i in newlist:
          aa.append(''.join(i))
      print(aa)
      
      		
      Reply
  12. Naveen Kumar says

    July 19, 2020 at 11:11 am

    question no.10

    list1 = [5, 20, 15, 20, 25, 50, 20]
    k=len(list1)
    k=list1.count(20)
    for i in range(k):
        list1.remove(20)
    print(list1)
    Reply
    • Ravneet says

      July 23, 2020 at 9:18 pm

      Thanks..it really helps:-)

      Reply
    • nandish says

      July 25, 2020 at 12:12 pm

      list1 = [5, 20, 15, 20, 25, 50, 20]
      list2=list(set(list1))
      list2.remove(20)
      print(list2)
      Reply
  13. sankar nath says

    July 17, 2020 at 7:52 pm

    Questions 4:

    
    list1 = ["Hello", "Take"]
    list2 = ["Dear", "sir"]
    
    print( list1[0]+ " "+list2[0], list1[0]+ " "+ list2[1], list1[1]+" "+list2[0], list1[1]+list2[1] )
    Reply
  14. bharanisampath says

    July 16, 2020 at 12:20 pm

    Any alternate answers for Q-10

    Reply
    • Naveen Kumar says

      July 19, 2020 at 11:09 am

      Here is an alternate solution for question no.10

      list1 = [5, 20, 15, 20, 25, 50, 20]
      k=len(list1)
      k=list1.count(20)
      for i in range(k):
          list1.remove(20)
      print(list1)
      Reply
      • Hi says

        November 21, 2020 at 3:15 pm

        list1 = [5, 20, 15, 20, 25, 50, 20]
        for x in list1:
            if x ==20:
                list1.remove(20)
        print(list1)
        Reply
    • nandish says

      July 25, 2020 at 12:16 pm

      list1 = [5, 20, 15, 20, 25, 50, 20]
      list2=list(set(list1))
      list2.remove(20)
      print(list2)
      Reply
    • Adarsh Anand says

      November 8, 2020 at 11:43 am

      
      list1 = [5, 20, 15, 20, 25, 50, 20]
      
      for item in list1:
          if item == 20:
              c = list1.index(item)
              list1.pop(c)
      
      print(list1)
      
      Reply
  15. Alec Nigh says

    June 24, 2020 at 12:08 am

    Alternative answer to Q9 — Note: would have convert back to a list to satisfy the question requirements.

    
    list1 = [5, 10, 15, 20, 25, 50, 20]
    list1 = " ".join(map(str,list1))
    list2 = list1.replace("20", "200", 1)
    print(list2)
    
    Reply
  16. Qadeer Rizvi says

    June 23, 2020 at 3:33 am

    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.

    Reply
    • Vishal says

      June 25, 2020 at 3:08 pm

      You’re welcome, Qadeer Rizvi.

      Reply
  17. Bostan Khan says

    June 14, 2020 at 7:38 pm

    sir your article are amazing very helpful for me.keep it up and live long

    Reply
    • Vishal says

      June 15, 2020 at 5:01 pm

      Thank you, Bostan Khan, for your kind words

      Reply
  18. shubham kumar singh says

    June 13, 2020 at 1:07 pm

    list1[2][1][2].extend(subList)
    i can’t understand the indexing value.please heip me out.

    Reply
    • shivam pal says

      June 17, 2020 at 11:08 am

      list1[2][1][2].extend(["h", "i", "j"])
      Reply
    • Paweł says

      November 12, 2020 at 12:43 am

      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.

      Reply
  19. Monika Gupta says

    June 11, 2020 at 11:13 am

    can you explain Q4 flow of code not able to understand

    Reply
    • Bostan Khan says

      June 14, 2020 at 7:33 pm

      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

      Reply
  20. Prashanth says

    May 29, 2020 at 8:14 pm

    Good Work Vishal !! Do you run any online classes for Python. I am ready to join in your class

    Reply
    • Vishal says

      May 31, 2020 at 11:13 am

      Hey Prashanth, I am not running any classes as of now. I will definitely let you know once I started it.

      Reply
  21. Alec Nigh says

    May 29, 2020 at 12:02 am

    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!

    Reply
  22. Marcin says

    May 26, 2020 at 9:49 pm

    Question 7

    list1[2][2].append(7000)

    Question 8

            
    list1[2][1][2].extend(subList)

    I don’t understand the index of lists. Where they come from? Does someone want to explain?

    Reply
    • Bostan Khan says

      June 14, 2020 at 7:36 pm

      its like array indexing access ,the point here to be noted each list in the list will be consider an element of the list

      Reply
    • Dalvir says

      July 31, 2020 at 4:29 pm

      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.

      Reply
  23. Gayathri says

    May 16, 2020 at 3:49 pm

    for question1, can we use the below code?

    start = 100
    stop = 500
    step = 100
    stop += step
    for i in reversed(range(start, stop, step)):
        print(i, end=" ")
    

    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

    Reply
    • Ashish says

      July 24, 2020 at 7:22 am

      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.

      Reply
  24. Shahnawaz says

    May 8, 2020 at 4:12 pm

    question 5: i think this method is more easy and if this method is not preferred then pls reply

    list1 = [10,20,30,40]
    list2 = [100,200,300,400]
    l2.reverse()
    
    for value in range(len(l1)):
        print(list1[value],list2[value])
    
    Reply
    • shivam pal says

      June 17, 2020 at 11:13 am

      
      for i in list1,list2[::-1]:
        print(i)
      
      Reply
  25. Noma says

    May 1, 2020 at 5:24 am

    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 ?

    Reply
    • Vishal says

      May 4, 2020 at 9:56 am

      Hey Noma, Please use the following code

      list1 = [10, 20, [300, 400, [5000, 6000], 500], 30, 40]
      for i in list1:
          if isinstance(i, list):
              continue
          else:
              print(i)
      Reply
    • Yashu gaur says

      August 1, 2020 at 1:33 pm

      Thanks sir it gave a lot of help to me. Thanks again

      Reply
  26. Pallavi says

    April 15, 2020 at 5:38 pm

    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

    Reply
    • Vishal says

      April 15, 2020 at 9:25 pm

      Thank you, Pallavi. Within two weeks you will see new topics for beginners on PYnative

      Reply

Leave a Reply Cancel reply

your email address will NOT be published. all comments are moderated according to our comment policy.

Use <pre> tag for posting code. E.g. <pre> Your code </pre>

 Python Exercises

  • Python Exercises Home
  • Basic Exercise for Beginners
  • Input and Output Exercise
  • Loop Exercise
  • Functions Exercise
  • String Exercise
  • Data Structure Exercise
  • List Exercise
  • Dictionary Exercise
  • Set Exercise
  • Tuple Exercise
  • Date and Time Exercise
  • OOP Exercise
  • Python JSON Exercise
  • Random Data Generation Exercise
  • NumPy Exercise
  • Pandas Exercise
  • Matplotlib Exercise
  • Python Database Exercise

All Python Topics

Python Basics Python Exercises Python Quizzes Python Regex Python Random Python Pandas Python Databases Python MySQL Python PostgreSQL Python SQLite Python JSON
TweetF  sharein  shareP  Pin

About PYnative

PYnative.com is for Python lovers. Here, You can get Tutorials, Exercises, and Quizzes to practice and improve your Python skills.

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Legal Stuff

  • About Us
  • Privacy Policy
  • Cookie Policy
  • Terms Of Use
  • Contact Us
DMCA.com Protection Status

Copyright © 2018-2021 · [pynative.com]