Answer:
I am writing a Python function unique()
def unique(list): # function unique that takes a list as parameter
unique_list = [] #list to store unique elements
for elements in list: # loop that checks every element of the list
if elements not in unique_list: # takes unique elements from list
unique_list.append(elements)
#appends unique elements from list to unique_list
return unique_list #outputs unique_list elements
Explanation:
The unique() function takes a list as argument which is named as list.
unique_list is a new list which stores unique element from the list.
The loop moves through the elements of the list one by one.
if condition checks if the element in list is not present in the unique_list which means that element is unique to the unique_list.
If this condition is true this means that the element is not repeated and is not already present in unique_list. Then that element is included to the unique_list using append() function which appends an element into the unique_list from the list.
If you want to check if this function works you pass a list with repeated elements to this function so that it can print the unique elements as follows:
print(unique([1,2,2,2,2,3,4,4,4,4,4,5]))
Output:
[1, 2, 3, 4, 5]
The screen shot of the function along with its output is attached.