Exercise 13
Write a Python algorithm as a function to remove duplicate elements from a list.
Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
def removeDuplicate(L): # Initialize the list without duplicate elements noDuplicate = [] # Iterate through the elements of L and remove duplicates for x in L: if x not in noDuplicate: noDuplicate.append(x) return noDuplicate # Example L = [5, 19, 5, 21, 5, 13, 21, 5] print("List without duplicate elements:", removeDuplicate(L)) # Output: List without duplicate elements: [5, 19, 21, 13] |
Younes Derfoufi
CRMEF OUJDA
1 thought on “Solution Exercise 13: Python algorithm to remove duplicate elements from a list”