- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if list contains all unique elements in Python
A list in python can contain elements all of which may or may not be unique. But for a scenario when we need unique elements like marking the attendance for different roll numbers of a class. Below is the approaches with can use.
With Set()
A python set is a collection which is unordered, unindexed and also contains unique elements. So we will compare the length of the set created from the list with the length of the list itself. They will be equal only if there are unique elements in the list.
Example
# Given List Alist = ['Mon','Tue','Wed'] print("The given list : ",Alist) # Compare length for unique elements if(len(set(Alist)) == len(Alist)): print("All elements are unique.") else: print("All elements are not unique.")
Output
Running the above code gives us the following result −
The given list : ['Mon', 'Tue', 'Wed'] All elements are unique.
Running the same program again without having unique elements.
Example
# Given List Alist = ['Mon','Tue','Wed','Mon'] print("The given list : ",Alist) # Compare length for unique elements if(len(set(Alist)) == len(Alist)): print("All elements are unique.") else: print("All elements are not unique.")
Output
Running the above code gives us the following result −
The given list : ['Mon', 'Tue', 'Wed', 'Mon'] All elements are not unique.
With count()
We can also use the in-built count() which will count the frequency of each element in the list. If the count is greater than 1 then we have duplicates in the list.
Example
# Given List list1 = ['Mon','Tue','Wed','Mon'] list2 = ['Mon','Tue','Wed'] def dupcheck(x): for elem in x: if x.count(elem) > 1: return True return False if dupcheck(list1): print("The given list : ", list1) print("There are duplicates.") else: print("The given list : ", list1) print("No duplicates.") if dupcheck(list2): print("The given list : ", list2) print("There are duplicates.") else: print("The given list : ", list2) print("No duplicates.")
Output
Running the above code gives us the following result −
The given list : ['Mon', 'Tue', 'Wed', 'Mon'] There are duplicates. The given list : ['Mon', 'Tue', 'Wed'] No duplicates.
- Related Articles
- Python program to check if a string contains all unique characters
- Python - Check if all elements in a List are same
- Python - Check if all elements in a list are identical
- Check if an array contains all elements of a given range in Python
- Check if list contains consecutive numbers in Python
- Python program to check if a string contains any unique character
- Check if all array elements are distinct in Python
- Python – Check if elements index are equal for list elements
- Python – Test if all elements are unique in columns of a Matrix
- Checking if a string contains all unique characters using JavaScript
- Python – Check if elements in a specific index are equal for list elements
- Check if object contains all keys in JavaScript array
- Check if elements of Linked List are present in pair in Python
- How to find unique permutations if a vector contains repeated elements in R?
- Python Program to check whether all elements in a string list are numeric
