Introduction
In Python, you can easily check if all elements in a list are identical using a simple function. In this challenge, you will be tasked with creating a function that checks if all elements in a list are identical.
This tutorial is from open-source community. Access the source code
In Python, you can easily check if all elements in a list are identical using a simple function. In this challenge, you will be tasked with creating a function that checks if all elements in a list are identical.
Write a function all_equal(lst)
that takes a list as an argument and returns True
if all elements in the list are identical, and False
otherwise.
To solve this problem, you can use the following steps:
set()
to eliminate duplicate elements in the list.len()
to check if the length of the set is 1
.1
, return True
. Otherwise, return False
.def all_equal(lst):
return len(set(lst)) == 1
all_equal([1, 2, 3, 4, 5, 6]) ## False
all_equal([1, 1, 1, 1]) ## True
In this challenge, you learned how to check if all elements in a list are identical using a simple function. You can use this function to quickly determine if a list contains only identical elements.