Here, we are implementing a python program to check whether all elements of a list are unique or not?
It's very simple to check, by following two steps
- Convert the list in a set (as you should know that set contains the unique elements) – it will remove the duplicate elements if any.
- Then, compare the length of the list and set – if both are the same then all elements are unique.
Program:
Output
x: [10, 20, 30, 40, 50] len(x): 5 set(x): {40, 10, 50, 20, 30} len(set(x)): 5 check_unique(x): True y: [10, 20, 20, 20, 20] len(y): 5 set(y): {10, 20} len(set(y)): 2 check_unique(y): False z: [10, 10, 10, 10, 10] len(z): 5 set(z): {10} len(set(z)): 1 check_unique(z): Falseneed an explanation for this answer? contact us directly to get an explanation for this answer