Unique Elements
Easy
17
77.3% Acceptance
In this lab, you are tasked with writing a Python function named unique_elements
. This function should take a list as input and return a new list that contains only the unique elements from the input list, preserving their original order.
Function Requirements:
- Input: A list of elements (can be of any type).
- Output: A new list with unique elements from the input list, maintaining the original order.
Examples:
-
Example 1:
- Input:
[1, 2, 2, 3, 3, 3, 4]
- Output:
[1, 2, 3, 4]
- Here, duplicate elements
2
and3
are removed.
- Input:
-
Example 2:
- Input:
["apple", "banana", "apple", "orange", "banana"]
- Output:
["apple", "banana", "orange"]
- In this case, the second occurrences of
"apple"
and"banana"
are removed.
- Input:
Edge Cases to Consider:
- Empty List: If the input list is empty, the function should return
None
. - Type Validation: The function should only accept a list as input. If any other type of argument is passed, the function must raise a
TypeError
.
By carefully handling these scenarios and following the above requirements, you will create a function that effectively filters out duplicate elements while preserving the order of unique elements in a list.