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: December 8, 2021 | 171 Comments

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.

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. 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]

Expected output:

[500, 400, 300, 200, 100]
Show Hint

Use the list function reverse()

Show Solution

Solution 1: list function reverse()

list1 = [100, 200, 300, 400, 500]
list1.reverse()
print(list1)

Solution 2: Using negative slicing

-1 indicates to start from the last item.

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

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"]

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.

list1 = ["M", "na", "i", "Ke"] 
list2 = ["y", "me", "s", "lly"]
list3 = [i + j for i, j in zip(list1, list2)]
print(list3)

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]

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.
numbers = [1, 2, 3, 4, 5, 6, 7]
# result list
res = []
for i in numbers:
    # calculate square and add to the result list
    res.append(i * i)
print(res)

Solution 2: Use list comprehension

numbers = [1, 2, 3, 4, 5, 6, 7]
res = [x * x for x in numbers]
print(res)

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 Hint

Use a list comprehension to iterate two lists using a for loop and concatenate the current item of each list.

Show Solution
list1 = ["Hello ", "take "]
list2 = ["Dear", "Sir"]

res = [x + y for x in list1 for y in list2]
print(res)

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]

Expected output:

10 400
20 300
30 200
40 100
Show Hint

Use the zip() function. This function takes two or more iterables (like list, dict, string), aggregates them in a tuple, and returns it.

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 a list2[::-1] (reverse list using list slicing)
  • Iterate the result using a for loop
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 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

list1 = ["Mike", "", "Emma", "Kelly", "", "Brad"]

# remove None from list1 and convert result into list
res = list(filter(None, list1))
print(res)

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]

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

list1 = [10, 20, [300, 400, [5000, 6000], 500], 30, 40]

# understand indexing
# list1[0] = 10
# list1[1] = 20
# list1[2] = [300, 400, [5000, 6000], 500]
# list1[2][2] = [5000, 6000]
# list1[2][2][1] = 6000

# solution
list1[2][2].append(7000)
print(list1)

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"]

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
list1 = ["a", "b", ["c", ["d", "e", ["f", "g"], "k"], "l"], "m", "n"]
sub_list = ["h", "i", "j"]

# understand indexing
# list1[2] = ['c', ['d', 'e', ['f', 'g'], 'k'], 'l']
# list1[2][1] = ['d', 'e', ['f', 'g'], 'k']
# list1[2][1][2] = ['f', 'g']

# solution
list1[2][1][2].extend(sub_list)
print(list1)

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]

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
list1 = [5, 10, 15, 20, 25, 50, 20]

# get the first occurrence index
index = list1.index(20)

# update item present at location
list1[index] = 200
print(list1)

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]

Expected output:

[5, 15, 25, 50]
Show Solution

Solution 1: Use the list comprehension

list1 = [5, 20, 15, 20, 25, 50, 20]

# list comprehension
# remove specific items and return a new list
def remove_value(sample_list, val):
    return [i for i in sample_list if i != val]

res = remove_value(list1, 20)
print(res)

Solution 2: while loop (slow solution)

list1 = [5, 20, 15, 20, 25, 50, 20]

while 20 in list1:
    list1.remove(20)
print(list1)

Filed Under: Python, Python Basics, Python Exercises

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:

Python Python Basics 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

Posted In

Python Python Basics Python Exercises
TweetF  sharein  shareP  Pin

 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 File Handling Python OOP Python Date and Time Python Random Python Regex Python Pandas Python Databases Python MySQL Python PostgreSQL Python SQLite Python JSON

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
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our Terms Of Use, Cookie Policy, and Privacy Policy.

Copyright © 2018–2023 pynative.com