Ways to check all list item are same in Python

Python List

In this post, we are going to learn different ways to check all list item are same in Python. We are using a loop, set, list. count(), list slicing, and many more with code examples.

We are using these techniques to Checking Python check if all items in the list equal a value

1. Using loop to check all list item are same in Python

To check all elements of a list are equal to given value, basic method that always get our mind is to use a loop to loop over all elements and compare them with given value . But this way of checking given elements is legthly and time assuming.

Code Example

animal_list = ['Cat','Cat','Cat','Cat','rat','Cat','Cat']

#function to check given value equal to elements in the list

def chk_allsame(list):
    all_equal = True
    first_item = list[0]
    for element in list:
      if first_item != element:
        all_equal = False  
        #return all_equal
        break;

    if(all_equal):
     print('all element are equal')
    else:
     print('all element are not equal')

#calling the above-defined function

chk_allsame(animal_list)

Output

all element are not equal

2. set() to check all list item are same in Python

Set is a data structure in python that does not contain duplicate values. So we can easily check that all elements are the same in the list by converting the list to a set and check its length. If all elements are the same then the length of the set will be 1.it is a faster way to check if all items in the list equal a value with concise code.

Let understand with an example how to do so

Code Example

#Program using check if all items in the python list do equal  given value

animal_list = ['Cat','Cat','Cat','Cat','Rat','Cat','Cat']

if(len(set(animal_list)) == 1):

  print('all elements have same value')

else:

  print('all elements have not same value')

Output

all elements have same value

3. list.count() to check all list item are same in Python

The list built-in count() method returns the count of all occurrence of elements in the list. If the count of an element all occurrence is equal to the length of the list that means a list of all elements are the same else elements are not the same.

let understand with an example,Find if all items in a Python list equal a given value

Code Example

#code to find if all items in a Python list are equal to a given value

animal_list = ['Cat','Cat','Cat','Cat','Cat','Cat','Cat']

all_equal = False;

if len(animal_list) > 0 :

    all_equal = animal_list.count(animal_list[0]) == len(animal_list)

if all_equal:

  print('all elements have same value')

else:
  print('all elements have not same value')

Output

all elements have same value

4.all() to check all list item are same in Python

The all() function is takes an iterable(list, tuple, dictionary) return TURE if all elements are equal or if iterable is empty else return FALSE. In the case of a dictionary iterable, the dictionary key is checked against a given value, not a dictionary value.

Let understand with example,how to use to find

Code Example

#code to check if all items in a Python list equal a given value
animal_list = ['Cat','Cat','Cat','Cat','Cat','Cat','Cat']


if all(item == animal_list[0] for item in animal_list):

  print('all elements have same value')

else:
  print('all elements have not same value')

Output

all elements have same value

5. list slicing check all list item are same in Python

In this code snippet, we are using list slicing to find all the elements of the list are equal to a given value.

Let understnad with example

Code Example

#code example of Find if all items in a Python list equal a given value

animal_list = ['Cat','Cat','Cat','Cat','Cat','Cat','Cat']

if animal_list[:-1] == animal_list[1:]:

    print('all elements have same value')
else:

  print('all elements have not same value')

Output

all elements have same value

Conclusion

In this code example,we have understood different ways to Find if all items in a Python list equal a given value.Now using any of these given method we can easily check if list elements are equal to given value.