Use set instead of slicing

This commit is contained in:
guru kiran
2020-10-11 12:38:10 +05:30
committed by Isabelle Viktoria Maciohsek
parent 3e5dc5c39c
commit 6f09a19637

View File

@ -5,11 +5,11 @@ tags: list,beginner
Checks if all elements in a list are equal.
- Use `[1:]` and `[:-1]` to compare all the values in the given list.
- Use `set()` to eliminate duplicate elements and then use `len()` to check if length is 1.
```py
def all_equal(lst):
return lst[1:] == lst[:-1]
return len(set(lst)) == 1
```
```py