Exercise 1: Reverse a list in Python
# Solution:
my_list = [1, 2, 3, 4, 5]
my_list.reverse()
print("Reversed List:", my_list)
Exercise 2: Concatenate two lists index-wise
# Solution:
list1 = ["a", "b", "c"]
list2 = ["x", "y", "z"]
result = [i + j for i, j in zip(list1, list2)]
print("Concatenated List:", result)
Exercise 3: Turn every item of a list into its square
# Solution:
my_list = [1, 2, 3, 4, 5]
squared_list = [x**2 for x in my_list]
print("Squared List:", squared_list)
Exercise 4: Concatenate two lists in the following order
# Solution:
list1 = ["Hello", "Good"]
list2 = ["World", "Morning"]
result = [x + " " + y for x in list1 for y in list2]
print("Concatenated Order List:", result)
Exercise 5: Iterate both lists simultaneously
# Solution:
list1 = [1, 2, 3]
list2 = ["a", "b", "c"]
for x, y in zip(list1, list2):
print(x, y)
Exercise 6: Remove empty strings from the list of strings
# Solution:
string_list = ["apple", "", "banana", "", "cherry"]
filtered_list = [s for s in string_list if s]
print("Filtered List:", filtered_list)
Exercise 7: Add new item to list after a specified item
# Solution:
my_list = [1, 2, 3, 4]
new_item = 5
index = my_list.index(2) + 1
my_list.insert(index, new_item)
print("Updated List:", my_list)
Exercise 8: Extend nested list by adding the sublist
# Solution:
nested_list = [1, [2, 3], 4]
sublist = [5, 6]
nested_list[1].extend(sublist)
print("Extended Nested List:", nested_list)
Exercise 9: Replace list’s item with new value if found
# Solution:
my_list = ["apple", "banana", "cherry"]
old_value = "banana"
new_value = "blueberry"
index = my_list.index(old_value)
my_list[index] = new_value
print("Updated List:", my_list)
Exercise 10: Remove all occurrences of a specific item from a list
# Solution:
my_list = [1, 2, 3, 2, 4, 2]
item_to_remove = 2
filtered_list = [x for x in my_list if x != item_to_remove]
print("Filtered List:", filtered_list)