Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to identify if all numbers in list are the same or different if the list length is unknown (Python)? [duplicate]

Tags:

python

I'm using Python and I'm trying to figure out how to ascertain whether all numbers in a list are the same or different (even with just one integer being different) if beforehand I don't know the total number of elements in the list. Initially I wrote something like:

def equalOrNot(lst):
    if sum(lst)%len(lst)==0:
         return False
    else:
         return True

But it's not working in all cases. Any suggestions? Thanks

like image 843
Abdul Kader Kheirallah Avatar asked Sep 15 '25 02:09

Abdul Kader Kheirallah


1 Answers

Use set:

if len(set(lst)) == 1: 
like image 89
Kenly Avatar answered Sep 16 '25 15:09

Kenly