All equal
Define a function named all_equal()
that takes a list and checks whether all elements in
the list are the same.
For example:
calling all_equal([1, 1, 1]) should return True.
My solution
def all_equal(equal_list):
result = all(element == equal_list[0] for element in equal_list)
if (result):
return True
else:
return False
print(all_equal([1, 1, 1]))
Aother solution
def all_equal(items):
for item1 in items:
for item2 in items:
if item1 != item2:
return False
return True
All the best to you.
Top comments (3)
Nice challenge. May I add another solution using set()? I like to use it combined with equality operator:
Have a good one! :)
Nice solution 👌 Benny
You are the best
Could be wrong, but does this work? If everything in the list is identical, then the first number (or any number in the list, for that matter) must represent all numbers in the list.