Avatar ·

How to check if all elements are the same in a list?

📁 Advanced Python Topics

n = int(input())sp = []for _ in range(n):  sp.append(int(input()))

How to check if all numbers are the same in this list?

Avatar ·

First, it's worth formalizing what an "empty" element is.

Many elements can be considered "empty", for example,

[], (), {}, set(), ''

including

None, 0, 0.0, 0j, False

If you need to remove all such elements from a collection, you can use a list comprehension

[value for value in values if value]

Or, if a list is not required, a generator expression:

Log in to leave an answer

Blogs