Back to Python
2026-04-185 min read

List Exercises (Python Programming)

Learn List Exercises (Python Programming) step by step with clear examples and exercises.

Why This Matters

In this full guide on Python List Exercises, we will delve into the practical aspects of working with lists in Python. Mastery of these concepts is crucial for enhancing your programming skills and preparing you for various coding challenges. Lists provide a flexible way to store multiple items of the same type, making your code more concise, efficient, and readable.

Why This Matters

Lists are an essential data structure in Python that allows you to store and manipulate collections of data. Understanding how to effectively use lists will not only improve your problem-solving abilities but also make you a more proficient programmer. This tutorial will cover the core concepts, provide real-world examples, and discuss common mistakes to help you excel in exams, interviews, and real-life coding scenarios.

Prerequisites

Before diving into the core concept, ensure you have a solid understanding of the following:

  1. Basic Python syntax (variables, operators, and expressions)
  2. Control structures (if-else statements, loops)
  3. Functions and modules
  4. Data types (strings, integers, floats, booleans)
  5. Understanding of functions such as len(), min(), max(), and built-in mathematical operations
  6. Knowledge of string manipulation techniques like slicing, concatenation, and formatting

Core Concept

Creating Lists

To create a list in Python, you can use square brackets [] and separate items with commas:

my_list = [1, "apple", 3.14, True]
print(my_list) # Output: [1, 'apple', 3.14, True]

Accessing List Elements

You can access list elements by their index (starting at 0):

print(my_list[0]) # Output: 1
print(my_list[2]) # Output: 3.14

Modifying List Elements

To modify a list element, simply assign a new value to the corresponding index:

my_list[0] = "banana"
print(my_list) # Output: ['banana', 'apple', 3.14, True]

Adding Elements to Lists

To add an element at the end of a list, you can use the append() method:

my_list.append("orange")
print(my_list) # Output: ['banana', 'apple', 3.14, True, 'orange']

Inserting Elements into Lists

To insert an element at a specific index, use the insert() method:

my_list.insert(1, "kiwi")
print(my_list) # Output: ['banana', 'kiwi', 'apple', 3.14, True, 'orange']

Removing Elements from Lists

To remove an element by its index, use the remove() method:

my_list.remove("kiwi")
print(my_list) # Output: ['banana', 'apple', 3.14, True, 'orange']

Deleting Elements by Value

To delete all occurrences of a specific value, you can use a loop and remove() method:

while "apple" in my_list:
my_list.remove("apple")
print(my_list) # Output: ['banana', 3.14, True, 'orange']

Checking List Membership

To check if a list contains a specific value, you can use the in keyword:

if "apple" in my_list:
print("my_list contains apple")

Worked Example

Let's create a program that finds the second-highest number in a list of integers. We will use two variables, max_val and second_max, to store the maximum and second-maximum values respectively:

numbers = [5, 7, 2, 9, 8, 1, 3]
max_val = numbers[0]
second_max = None

for num in numbers:
if num > max_val and (second_max is None or num < second_max):
second_max = max_val
max_val = num
elif num > second_max and num != max_val:
second_max = num

if second_max is not None:
print(f"The second-highest number is {second_max}")
else:
print("All numbers are unique.")

Common Mistakes

  1. Forgetting to initialize second_max in the worked example (it should be set to None)
  2. Using a loop without updating the maximum or second-maximum values
  3. Comparing floating-point numbers with strict equality (use == instead of =)
  4. Misunderstanding list indexing (remember, indices start at 0)
  5. Trying to access an index that is out of range
  6. Forgetting to handle cases where the list contains only one unique number
  7. Not using proper indentation in loops and conditional statements
  8. Incorrectly handling edge cases such as empty lists or lists with a single element

Practice Questions

  1. Write a program that finds the sum of all even numbers in a list of integers.
  2. Create a function that reverses a given list.
  3. Given two lists, write a program that merges them into a single sorted list.
  4. Write a program that finds the frequency of each word in a given list of strings.
  5. Create a function that removes duplicates from a list while preserving the order of unique elements.
  6. Write a function to find the kth largest number in a list.
  7. Given a list of tuples representing points in a 2D plane, write a program to find the point with the maximum x-coordinate and the minimum y-coordinate.
  8. Write a program that finds all permutations of a given list.
  9. Create a function to check if a given list is a palindrome (reads the same forwards and backwards).
  10. Given a list of strings, write a program to sort them alphabetically while maintaining the original case (i.e., preserving uppercase and lowercase letters separately).

FAQ

  1. Why can't I use the del keyword to delete an element by index?
  • The del keyword is used to delete entire objects, not individual elements in lists. Instead, you should use the remove() method or slice assignment (e.g., my_list[index:index] = []).
  1. Can I create a list with mixed data types?
  • Yes! Python allows you to store different data types within the same list.
  1. What happens if I try to access an index that is out of range?
  • If you try to access an index that is out of range, Python will raise an IndexError. To avoid this, make sure to check the length of your list before attempting to access an index.
  1. How can I find the index of a specific element in a list?
  • You can use the index() method to find the index of an element in a list: my_list.index(element). If the element is not found, Python will raise a ValueError.
  1. What are some common list methods I should know about?
  • In addition to those mentioned earlier, some other useful list methods include sort(), count(), reverse(), and slicing (e.g., my_list[start:end]).
List Exercises (Python Programming) | Python | XQA Learn