Use set instead of slicing
This commit is contained in:
committed by
Isabelle Viktoria Maciohsek
parent
3e5dc5c39c
commit
6f09a19637
@ -5,11 +5,11 @@ tags: list,beginner
|
|||||||
|
|
||||||
Checks if all elements in a list are equal.
|
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
|
```py
|
||||||
def all_equal(lst):
|
def all_equal(lst):
|
||||||
return lst[1:] == lst[:-1]
|
return len(set(lst)) == 1
|
||||||
```
|
```
|
||||||
|
|
||||||
```py
|
```py
|
||||||
|
|||||||
Reference in New Issue
Block a user