Python – List Exercises

Here are some Python list exercises to help you practice and strengthen your understanding of working with lists:

Exercise 1: Reverse a List

Write a function that takes a list as input and returns a new list with the elements in reverse order.

def reverse_list(lst):
    return lst[::-1]

# Test
my_list = [1, 2, 3, 4, 5]
print(reverse_list(my_list))  # Output: [5, 4, 3, 2, 1]

Exercise 2: Remove Duplicates from a List

Write a function that removes all duplicate elements from a list, returning a list with only unique values.

def remove_duplicates(lst):
    return list(set(lst))

# Test
my_list = [1, 2, 2, 3, 4, 4, 5]
print(remove_duplicates(my_list))  # Output: [1, 2, 3, 4, 5]

Exercise 3: Sum of List Elements

Write a function that returns the sum of all elements in a list.

def sum_of_list(lst):
    return sum(lst)

# Test
my_list = [1, 2, 3, 4, 5]
print(sum_of_list(my_list))  # Output: 15

Exercise 4: Find the Largest Element

Write a function that finds and returns the largest element in a list.

def largest_element(lst):
    return max(lst)

# Test
my_list = [1, 2, 3, 4, 5]
print(largest_element(my_list))  # Output: 5

Exercise 5: Count the Occurrences of an Element

Write a function that counts how many times a given element appears in a list.

def count_occurrences(lst, element):
    return lst.count(element)

# Test
my_list = [1, 2, 2, 3, 4, 4, 5]
print(count_occurrences(my_list, 2))  # Output: 2

Exercise 6: Merge Two Lists

Write a function that merges two lists into one.

def merge_lists(lst1, lst2):
    return lst1 + lst2

# Test
list1 = [1, 2, 3]
list2 = [4, 5, 6]
print(merge_lists(list1, list2))  # Output: [1, 2, 3, 4, 5, 6]

Exercise 7: Flatten a Nested List

Write a function that takes a list of lists and returns a flat list with all the elements.

def flatten_list(lst):
    return [item for sublist in lst for item in sublist]

# Test
nested_list = [[1, 2], [3, 4], [5, 6]]
print(flatten_list(nested_list))  # Output: [1, 2, 3, 4, 5, 6]

Exercise 8: Find the Common Elements Between Two Lists

Write a function that finds the common elements between two lists.

def common_elements(lst1, lst2):
    return list(set(lst1) & set(lst2))

# Test
list1 = [1, 2, 3, 4]
list2 = [3, 4, 5, 6]
print(common_elements(list1, list2))  # Output: [3, 4]

Exercise 9: Create a List of Squares

Write a function that returns a list containing the square of each number from 1 to n.

def squares(n):
    return [i**2 for i in range(1, n+1)]

# Test
print(squares(5))  # Output: [1, 4, 9, 16, 25]

Exercise 10: Find the Second Largest Element

Write a function that returns the second largest element in a list.

def second_largest(lst):
    lst_sorted = sorted(lst, reverse=True)
    return lst_sorted[1] if len(lst_sorted) > 1 else None

# Test
my_list = [1, 2, 3, 4, 5]
print(second_largest(my_list))  # Output: 4

Exercise 11: Rotate a List

Write a function that rotates a list to the right by n positions.

def rotate_list(lst, n):
    n = n % len(lst)  # Handle cases where n is larger than the list length
    return lst[-n:] + lst[:-n]

# Test
my_list = [1, 2, 3, 4, 5]
print(rotate_list(my_list, 2))  # Output: [4, 5, 1, 2, 3]

Exercise 12: Check if List is Sorted

Write a function that checks if a list is sorted in ascending order.

def is_sorted(lst):
    return lst == sorted(lst)

# Test
my_list = [1, 2, 3, 4, 5]
print(is_sorted(my_list))  # Output: True

Exercise 13: Remove the N-th Element

Write a function that removes the element at a specified index n from the list.

def remove_nth_element(lst, n):
    return lst[:n] + lst[n+1:]

# Test
my_list = [1, 2, 3, 4, 5]
print(remove_nth_element(my_list, 2))  # Output: [1, 2, 4, 5]

Exercise 14: Check for Palindrome

Write a function that checks if a list is a palindrome (i.e., reads the same backward as forward).

def is_palindrome(lst):
    return lst == lst[::-1]

# Test
my_list = [1, 2, 3, 2, 1]
print(is_palindrome(my_list))  # Output: True

Exercise 15: Multiply Corresponding Elements of Two Lists

Write a function that multiplies corresponding elements of two lists and returns a new list with the products.

def multiply_lists(lst1, lst2):
    return [a * b for a, b in zip(lst1, lst2)]

# Test
list1 = [1, 2, 3]
list2 = [4, 5, 6]
print(multiply_lists(list1, list2))  # Output: [4, 10, 18]

Summary:

These exercises cover a wide range of basic and intermediate list operations. They will help you practice manipulating lists, sorting, finding elements, and combining lists. You can test these exercises with different inputs and try to extend or modify them to explore more functionalities!

Leave a Reply 0

Your email address will not be published. Required fields are marked *