Here are some sample Python programs that focus on lists and their various operations. These exercises cover a wide range of topics such as list manipulation, sorting, filtering, and basic algorithms:
1. Sum of All Elements in a List
Objective: Calculate the sum of all elements in a list.
def sum_of_list(lst):
return sum(lst)
numbers = [10, 20, 30, 40, 50]
print(f"The sum of the list is: {sum_of_list(numbers)}")
2. Find the Maximum and Minimum Element in a List
Objective: Find the largest and smallest elements in a list.
def find_max_min(lst):
return max(lst), min(lst)
numbers = [1, 2, 3, 4, 5, 100, 99]
maximum, minimum = find_max_min(numbers)
print(f"The maximum element is {maximum}")
print(f"The minimum element is {minimum}")
3. Reverse a List
Objective: Reverse a list without using built-in methods.
def reverse_list(lst):
return lst[::-1]
numbers = [1, 2, 3, 4, 5]
print(f"Reversed list: {reverse_list(numbers)}")
4. Remove Duplicates from a List
Objective: Remove duplicate elements from a list.
def remove_duplicates(lst):
return list(set(lst))
numbers = [1, 2, 2, 3, 4, 4, 5]
print(f"List after removing duplicates: {remove_duplicates(numbers)}")
5. Count Occurrences of an Element in a List
Objective: Count how many times an element appears in a list.
def count_occurrences(lst, element):
return lst.count(element)
numbers = [1, 2, 2, 3, 4, 2, 5]
print(f"The element 2 appears {count_occurrences(numbers, 2)} times.")
6. Find Even and Odd Numbers in a List
Objective: Separate even and odd numbers into two different lists.
def separate_even_odd(lst):
even = [num for num in lst if num % 2 == 0]
odd = [num for num in lst if num % 2 != 0]
return even, odd
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even, odd = separate_even_odd(numbers)
print(f"Even numbers: {even}")
print(f"Odd numbers: {odd}")
7. Merge Two Lists
Objective: Merge two lists into one.
def merge_lists(lst1, lst2):
return lst1 + lst2
list1 = [1, 2, 3]
list2 = [4, 5, 6]
print(f"Merged list: {merge_lists(list1, list2)}")
8. Sort a List in Ascending Order
Objective: Sort a list in ascending order without using the built-in sort()
method.
def sort_list(lst):
for i in range(len(lst)):
for j in range(i + 1, len(lst)):
if lst[i] > lst[j]:
lst[i], lst[j] = lst[j], lst[i]
return lst
numbers = [5, 3, 8, 6, 7, 2]
print(f"Sorted list: {sort_list(numbers)}")
9. Find the Second Largest Element in a List
Objective: Find the second largest element in a list.
def second_largest(lst):
unique_lst = list(set(lst)) # Removing duplicates
unique_lst.sort()
return unique_lst[-2] if len(unique_lst) >= 2 else None
numbers = [1, 2, 3, 4, 5, 5]
print(f"The second largest element is: {second_largest(numbers)}")
10. Flatten a Nested List
Objective: Flatten a list of lists into a single list.
def flatten_list(lst):
flat_list = []
for sublist in lst:
for item in sublist:
flat_list.append(item)
return flat_list
nested_list = [[1, 2], [3, 4], [5, 6]]
print(f"Flattened list: {flatten_list(nested_list)}")
11. Remove All Occurrences of an Element from a List
Objective: Remove all occurrences of a specific element from a list.
def remove_all_occurrences(lst, element):
return [item for item in lst if item != element]
numbers = [1, 2, 3, 4, 2, 5, 2]
print(f"List after removing all occurrences of 2: {remove_all_occurrences(numbers, 2)}")
12. Find Common Elements Between Two Lists
Objective: Find elements that are common in both lists.
def common_elements(lst1, lst2):
return list(set(lst1) & set(lst2))
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7]
print(f"Common elements: {common_elements(list1, list2)}")
13. Concatenate Strings in a List
Objective: Concatenate all strings in a list into a single string.
def concatenate_strings(lst):
return ''.join(lst)
words = ["Hello", " ", "World", "!"]
print(f"Concatenated string: {concatenate_strings(words)}")
14. Create a List of Squares
Objective: Generate a list containing the squares of numbers from 1 to n
.
def list_of_squares(n):
return [i**2 for i in range(1, n+1)]
n = int(input("Enter a number: "))
print(f"List of squares: {list_of_squares(n)}")
15. Find the Index of an Element in a List
Objective: Find the index of an element in a list.
def find_index(lst, element):
if element in lst:
return lst.index(element)
return -1 # Return -1 if element is not found
numbers = [10, 20, 30, 40, 50]
print(f"The index of 30 is: {find_index(numbers, 30)}")
0 comments:
Post a Comment