3 Ways to Check if all Elements in List are Same [Python Code]
When you think about this query, the first thing that comes to your mind is to run a loop and check if all elements in list are same.
Below is a simple program.
listChar = ['z','z','z','z'] nTemp = listChar bEqual = True for item in listChar: if nTemp != item: bEqual = False break; if bEqual: print "All elements in list are equal" else: print "All elements in list are not equal"
This code runs absolutely fine. And there is nothing wrong with this logic as you write it in many other programming languages. But, this is not the way you write this code in Python.
The list is one of the very prominent data structure in Python. And there are really interesting pieces of stuff you can code with simple two or three lines of code. For me, this is one of the finest reason to be in love with Python.
How to Check if all Elements in List are same in Python?
So, here you go to write the same program with simple logic in python.
Method 1: Using Python set:
Set is a collection type in Python, just like list and tuple (Ref: difference between list and tuple). Set is different as it can’t have duplicate elements, unlike list and tuple. All the elements in the set are unique.
So, the task is to find all the unique elements from the list.
When you find the set of the list, it removes all the duplicate elements.
Here is simple code with that you can check if all the elements of the list are same.
listChar = ['z','z','z','z'] if(len(set(listChar))==1): print "All elements in list are same" else: print "All elements in list are not same"
If the length of the set is one, all the elements in the given list are same. Otherwise, not.
Method 2: Using Python count() function:
The count() is the list object function which returns the number of occurrences of the input element.
To check if all elements in list are same, you can compare the number of occurrences of any elements in the list with the length of the list.
listChar = ['z','z','z','z'] if listChar.count(listChar) == len(listChar): print "All elements in list are same." else: print "Elements in list are different."
If the total count of occurrences of any element (first element as per above code) in the list is same as the length of the list, all the elements in the list are equal. Otherwise, elements in the list are different.
Method 3: Using Python all() function:
The all() is a function that takes iterable as an input and returns true if all the elements of the iterable are true. Otherwise, false.
The simple solution to our problem is – check if all elements in list are same as the first element in the list.
listChar = ['z','z','z','z'] if all(x == listChar for x in listChar): print "All elements in list are equal" else: print "All elements in list are not equal"
If all() function returns true means all the elements in the list are equal. Otherwise, not equal.
Note: If you are comparing two elements in Python, remember- comparing using ‘is’ and ‘==’ is different.
Some other tricky questions in Python, you can give a try:
- Randomly Select Item from List in Python (choice() method)
- Get all the Permutations of String
- Remove all 0 from a dictionary in Python
- Find the longest line from file in Python Program
This is all about this post. I am sure, you find some valuable solutions to your problem. If you ask me to choose one solution, using set() function (method 1) is pretty easy and easily understood.
If you think, we can check if all elements in list are same, even by the better way, write in a comment below. I would love that.
If you are interested in learning Python, don’t miss to read our complete Python tutorial.