# Python : Check if all elements in a List are same or matches a condition

In this article we will dicuss different ways to check if all element in a given List are same or matches a condition.

Suppose we have a list of string i.e.

```# List of string
listOfStrings = ['Hello'] * 10
```

Now let’s use python all() function to check if all elements in the given list are same.

#### Python : all() function

Python all() function checks if all Elements of given Iterable is True.

## check if element are same using all()

Let’s convert the list to Iterable and check if each entry of iterable is equal to first element of list using all() i.e.

```'''
check if element are same using all()
It will Iterate through all the elements in list and check if all elements are similar to first element or not.
'''
result = False;
if len(listOfStrings) > 0 :
result = all(elem == listOfStrings for elem in listOfStrings)

if result :
print("All Elements in List are Equal")
else:
print("All Elements in List are Not Equal")```

## Check if all elements are same using list.count()

count() returns the occurrence count of given element in the list.

Let’s call the count() function of list with firts element of list as argument. If its occurrence count is equal to the length of list, then it means all elements in list are Same i.e.

```'''
check if element are same using list.count()
If occurence count of first element in list is equal to length of list.
Then it means all elements in List are equal
'''
result = False;
if len(listOfStrings) > 0 :
result = listOfStrings.count(listOfStrings) == len(listOfStrings)```

Let’s do the same thing in single line i.e.
`result = len(listOfStrings) > 0 and all(elem == listOfStrings for elem in listOfStrings)`

## Check if all elements are same using Set

As set contains only unique elements, so convert the list to set. If set size is 1 then it means all elements in given list are same i.e.

```'''
As set contains unique elements only, so if list has similar elements, then only one will stored in set.
'''
result = len(set(listOfStrings)) == 1```

Complete example is as follows,
```def main():

# List of string
listOfStrings = ['Hello'] * 10

# Print the List
print(listOfStrings)

'''
check if element are same using all()
It will Iterate through all the elements in list and check if all elements are similar to first element or not.
'''
result = False;
if len(listOfStrings) > 0 :
result = all(elem == listOfStrings for elem in listOfStrings)

if result :
print("All Elements in List are Equal")
else:
print("All Elements in List are Not Equal")

'''
check if element are same using list.count()
If occurence count of first element in list is equal to length of list.
Then it means all elements in List are equal
'''
result = False;
if len(listOfStrings) > 0 :
result = listOfStrings.count(listOfStrings) == len(listOfStrings)

if result :
print("All Elements in List are Equal")
else:
print("All Elements in List are Not Equal")

# Do the above logic in single line
result = len(listOfStrings) > 0 and all(elem == listOfStrings for elem in listOfStrings)

if result :
print("All Elements in List are Equal")
else:
print("All Elements in List are Not Equal")

'''
As set contains unique elements only, so if list has similar elements, then only one will stored in set.
'''
result = len(set(listOfStrings)) == 1

if result :
print("All Elements in List are Equal")
else:
print("All Elements in List are Not Equal")

if __name__ == '__main__':
main()
```

Output:
```['Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello']
All Elements in List are Equal
All Elements in List are Equal
All Elements in List are Equal
All Elements in List are Equal
```

## Pandas Tutorials -Learn Data Analysis with Python

### Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

##### Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

##### Join a LinkedIn Community of Python Developers

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top