550

I need a function which takes in a list and outputs True if all elements in the input list evaluate as equal to each other using the standard equality operator and False otherwise.

I feel it would be best to iterate through the list comparing adjacent elements and then AND all the resulting Boolean values. But I'm not sure what's the most Pythonic way to do that.

Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
max
  • 49,282
  • 56
  • 208
  • 355
  • 6
    Equal as in `a == b` or identical as in `a is b`? – kennytm Oct 02 '10 at 07:35
  • 1
    Should the solution handle empty lists? If so, what should be returned? – Doug Oct 02 '10 at 07:43
  • 2
    Equal as in a == b. Should handle empty list, and return True. – max Oct 02 '10 at 08:21
  • 6
    Although I know it's slower than some of the other recommendations, I'm surprised `functools.reduce(operator.eq, a)` hasn't been suggested. – user2846495 Apr 05 '20 at 21:55
  • 1
    If the elements to compare are only numerical datatype, (Int, Double, Float). Given a List of numerical data type, extract the *minimum* and *maximum*, then return *maximum* == *minimum* and it is done. You can (over compute the returning) returning *maximum* - *minimum* == 0 – Franco Gil Sep 30 '20 at 02:21
  • @FrancoGil that requires iterating over the list twice. It would be slower than most (if not all) methods suggested here. And you'd need to check for the empty list. And like you said, it will silently give the wrong result for a list of strings. – Boris Verkhovskiy Nov 14 '20 at 07:03
  • 3
    @ user2846495 `functools.reduce(operator.eq, a)` would not work. for example with the list `[True, False, False]`, it would return `((True == False) == False)` which is `True`. Whereas the function should return False (because the elements are not all equal) – tbrugere Apr 19 '21 at 15:40
  • @Boris You can get both the maximum and minimum in one pass if you write your own loop instead of using Python's `max()` and `min()`, and further optimize it like https://www.siderite.dev/blog/finding-simultaneously-minimum-and.html/ – Leponzo Apr 28 '21 at 16:36
  • @Leponzo `max()` and `min()` are implemented in C on CPython. I think you would get a significant slowdown implementing it yourself. – Boris Verkhovskiy May 02 '21 at 22:10
  • @Boris I was referring to your comment [here](https://stackoverflow.com/questions/3844801/check-if-all-elements-in-a-list-are-identical?noredirect=1#comment114624538_3844801). – Leponzo May 03 '21 at 02:13
  • @Leponzo I'm saying if you implement getting the min/max in one pass in Python, it might still be faster to just call the functions and iterate over the list twice (for certain sized lists at least) because they are implemented in C. – Boris Verkhovskiy May 03 '21 at 09:16
  • 1
    why not just use `more_itertools.all_equal`? https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.all_equal – Michael L Aug 04 '21 at 21:57

30 Answers30

598

Use itertools.groupby (see the itertools recipes):

from itertools import groupby

def all_equal(iterable):
    g = groupby(iterable)
    return next(g, True) and not next(g, False)

or without groupby:

def all_equal(iterator):
    iterator = iter(iterator)
    try:
        first = next(iterator)
    except StopIteration:
        return True
    return all(first == x for x in iterator)

There are a number of alternative one-liners you might consider:

  1. Converting the input to a set and checking that it only has one or zero (in case the input is empty) items

    def all_equal2(iterator):
        return len(set(iterator)) <= 1
    
  2. Comparing against the input list without the first item

    def all_equal3(lst):
        return lst[:-1] == lst[1:]
    
  3. Counting how many times the first item appears in the list

    def all_equal_ivo(lst):
        return not lst or lst.count(lst[0]) == len(lst)
    
  4. Comparing against a list of the first element repeated

    def all_equal_6502(lst):
        return not lst or [lst[0]]*len(lst) == lst
    

But they have some downsides, namely:

  1. all_equal and all_equal2 can use any iterators, but the others must take a sequence input, typically concrete containers like a list or tuple.
  2. all_equal and all_equal3 stop as soon as a difference is found (what is called "short circuit"), whereas all the alternatives require iterating over the entire list, even if you can tell that the answer is False just by looking at the first two elements.
  3. In all_equal2 the content must be hashable. A list of lists will raise a TypeError for example.
  4. all_equal2 (in the worst case) and all_equal_6502 create a copy of the list, meaning you need to use double the memory.

On Python 3.9, using perfplot, we get these timings (lower Runtime [s] is better):

for a list with a difference in the first two elements, groupby is fastestfor a list with no differences, count(l[0]) is fastest

Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • 4
    Don't forget memory usage analysis for very large arrays, a native solution which optimizes away calls to `obj.__eq__` when `lhs is rhs`, and out-of-order optimizations to allow short circuiting sorted lists more quickly. – Glenn Maynard Oct 02 '10 at 08:31
  • 4
    In case the intuition on checkEqual3 wasn't immediately obvious to anyone else: all items are the same if `first == second and second == third and ...` – teichert May 19 '20 at 17:16
  • This is brilliantly fast. Clever approach too. Helpful suggestion for those implementing with `pytest`, you should return the result before asserting True/False. (Wrap this function in another.) – Yaakov Bressler Dec 20 '20 at 17:43
  • 2
    @Boris: What is the code for these charts? – ChaimG Jan 27 '21 at 05:51
  • 4
    @ChaimG if you click on "Edit", the code is hidden in a comment in the text of the answer. – Boris Verkhovskiy Jan 27 '21 at 12:10
  • You could make a further improvement to the version without `groupby` by using `eq` from the [operator](https://docs.python.org/3/library/operator.html) module. These functions will be slightly faster than writing it yourself because they are implemented in C within the interpreter. – txk2048 Mar 02 '21 at 15:01
  • In `all_equal` you can use `first = next(iterator, None)` to avoid `try/except` construction. – Stanislav Volodarskiy Oct 23 '21 at 14:31
  • I prefer pairwise comparison with zip(lst, lst[1:]) – Johannes Schaub - litb Jan 24 '22 at 19:49
  • To make this work with `pd.NA`, just [add an exception handler](https://stackoverflow.com/a/59543818/357313). – Michel de Ruiter Jan 25 '23 at 17:52
383

A solution faster than using set() that works on sequences (not iterables) is to simply count the first element. This assumes the list is non-empty (but that's trivial to check, and decide yourself what the outcome should be on an empty list)

x.count(x[0]) == len(x)

some simple benchmarks:

>>> timeit.timeit('len(set(s1))<=1', 's1=[1]*5000', number=10000)
1.4383411407470703
>>> timeit.timeit('len(set(s1))<=1', 's1=[1]*4999+[2]', number=10000)
1.4765670299530029
>>> timeit.timeit('s1.count(s1[0])==len(s1)', 's1=[1]*5000', number=10000)
0.26274609565734863
>>> timeit.timeit('s1.count(s1[0])==len(s1)', 's1=[1]*4999+[2]', number=10000)
0.25654196739196777
Ivo van der Wijk
  • 16,341
  • 4
  • 43
  • 57
  • 8
    OMG, this is 6 times faster than the set solution! (280 million elements/sec vs 45 million elements/sec on my laptop). Why??? And is there any way to modify it so that it short circuits (I guess not...) – max Oct 02 '10 at 09:18
  • 3
    I guess list.count has a highly optimized C implementation, and the length of the list is stored internally, so len() is cheap as well. There's not a way to short-circuit count() since you will need to really check all elements to get the correct count. – Ivo van der Wijk Oct 02 '10 at 10:01
  • Can I change it to: `x.count(next(x)) == len(x)` so that it works for any container x? Ahh.. nm, just saw that .count is only available for sequences.. Why isn't it implemented for other builtin containers? Is counting inside a dictionary inherently less meaningful than inside a list? – max Oct 05 '10 at 05:09
  • 5
    An iterator may not have a length. E.g. it can be infinite or just dynamically generated. You can only find its length by converting it to a list which takes away most of the iterators advantages – Ivo van der Wijk Oct 05 '10 at 05:51
  • Sorry, what I meant was why `count` isn't implemented for iterables, not why `len` isn't available for iterators. The answer is probably that it's just an oversight. But it's irrelavant for us because default `.count()` for sequences is very slow (pure python). The reason your solution is so fast is that it relies on the C-implemented `count` provided by `list`. So I suppose whichever iterable happens to implement `count` method in C will benefit from your approach. – max Mar 12 '16 at 03:36
  • @IvovanderWijk Among the faster approaches is this one: `timeit.timeit('all(i == s1[0] for i in set(s1))', 's1=[1]*5000', number=10000)`. It clocks in at 0.39 ms on my system while the count == len clocks in at 0.16 ms. – Aaron3468 Jan 29 '18 at 02:04
  • This unfortunately does not work for `numpy` arrays. – zyy Mar 23 '20 at 01:46
  • On my machine running 3.8.10, the difference is even starker - the `.count` approach is about 13 times as fast. – Karl Knechtel Aug 11 '22 at 07:49
231

[edit: This answer addresses the currently top-voted itertools.groupby (which is a good answer) answer later on.]

Without rewriting the program, the most asymptotically performant and most readable way is as follows:

all(x==myList[0] for x in myList)

(Yes, this even works with the empty list! This is because this is one of the few cases where python has lazy semantics.)

This will fail at the earliest possible time, so it is asymptotically optimal (expected time is approximately O(#uniques) rather than O(N), but worst-case time still O(N)). This is assuming you have not seen the data before...

(If you care about performance but not that much about performance, you can just do the usual standard optimizations first, like hoisting the myList[0] constant out of the loop and adding clunky logic for the edge case, though this is something the python compiler might eventually learn how to do and thus one should not do it unless absolutely necessary, as it destroys readability for minimal gain.)

If you care slightly more about performance, this is twice as fast as above but a bit more verbose:

def allEqual(iterable):
    iterator = iter(iterable)
    
    try:
        firstItem = next(iterator)
    except StopIteration:
        return True
        
    for x in iterator:
        if x!=firstItem:
            return False
    return True

If you care even more about performance (but not enough to rewrite your program), use the currently top-voted itertools.groupby answer, which is twice as fast as allEqual because it is probably optimized C code. (According to the docs, it should (similar to this answer) not have any memory overhead because the lazy generator is never evaluated into a list... which one might be worried about, but the pseudocode shows that the grouped 'lists' are actually lazy generators.)

If you care even more about performance read on...


sidenotes regarding performance, because the other answers are talking about it for some unknown reason:

... if you have seen the data before and are likely using a collection data structure of some sort, and you really care about performance, you can get .isAllEqual() for free O(1) by augmenting your structure with a Counter that is updated with every insert/delete/etc. operation and just checking if it's of the form {something:someCount} i.e. len(counter.keys())==1; alternatively you can keep a Counter on the side in a separate variable. This is provably better than anything else up to constant factor. Perhaps you can also use python's FFI with ctypes with your chosen method, and perhaps with a heuristic (like if it's a sequence with getitem, then checking first element, last element, then elements in-order).

Of course, there's something to be said for readability.

ninjagecko
  • 88,546
  • 24
  • 137
  • 145
  • This works, but it's a bit (1.5x) slower than @KennyTM `checkEqual1`. I'm not sure why. – max Apr 24 '12 at 17:20
  • 5
    max: Likely because I did not bother to perform the optimization `first=myList[0]` `all(x==first for x in myList)`, perhaps – ninjagecko Nov 17 '15 at 12:48
  • I think that myList[0] is evaluated with each iteration. >>> timeit.timeit('all([y == x[0] for y in x])', 'x=[1] * 4000', number=10000) 2.707076672740641 >>> timeit.timeit('x0 = x[0]; all([y == x0 for y in x])', 'x=[1] * 4000', number=10000) 2.0908854261426484 – Matt Liberty Jan 11 '16 at 21:35
  • 3
    I should of course clarify that the optimization `first=myList[0]` will throw an `IndexError` on an empty list, so commenters who were talking about that optimization I mentioned will have to deal with the edge-case of an empty list. However the original is fine (`x==myList[0]` is fine within the `all` because it is never evaluated if the list is empty). – ninjagecko Jan 13 '16 at 10:45
  • 1
    This is clearly the right way to to it. If you want speed in every case, use something like numpy. – Henry Gomersall May 06 '16 at 16:14
  • For null lists, do you mean this will return `False`? I'm not able to replicate if so. If you wish to detect a null list, how would you modify the above? I have been using `if myList and all(x==myList[0] for x in myList)`? – Josmoor98 Jul 22 '19 at 10:31
  • @Josmoor98: I mean the empty list `[]`, which I assume you mean too (if the list can be None, that's another issue and must be treated separately), and that `all(x==y[0] for x in myList)` will be true for inputs myList=... `[]`, `[1]`, `[1,1]`, `['a','a','a']`, `[[1],[1]]`, `[(1,),(1,)]`, and even strings '"zzz"'. It will be false for inputs like `[1,2]`, or anything where two elements a,b don't return `a.__eq__(b)` or equivalent. [continued] – ninjagecko Jul 23 '19 at 18:19
  • @Josmoor98: [continued] If your var can be None, be aware `bool([])==False` and `bool(None)==False`. Stylistically if were to encapsulate this answer in a function, such "are null" checks should be made outside the function e.g. `def allEqual(myList): all(.....)`, and later on `if myVar!=None and allEqual(myVar):` – ninjagecko Jul 23 '19 at 18:19
  • In my testing, this is much slower than the `set` approach for input that meets the criteria, which in turn is much slower than the `.count` approach. However, it is capable of short-circuiting. – Karl Knechtel Aug 11 '22 at 07:52
75

Convert your input into a set:

len(set(the_list)) <= 1

Using set removes all duplicate elements. <= 1 is so that it correctly returns True when the input is empty.

This requires that all the elements in your input are hashable. You'll get a TypeError if you pass in a list of lists for example.

Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
cbalawat
  • 1,133
  • 1
  • 9
  • 14
50

You can convert the list to a set. A set cannot have duplicates. So if all the elements in the original list are identical, the set will have just one element.

if len(set(input_list)) == 1:
    # input_list has all identical elements.
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
codaddict
  • 445,704
  • 82
  • 492
  • 529
  • this is nice but it doesn't short circuit and you have to calculate the length of the resulting list. – aaronasterling Oct 02 '10 at 07:44
  • @AaronMcSmooth: Still a noob in py. Don't even know what a short circut in py means :) – codaddict Oct 02 '10 at 07:55
  • 3
    @codaddict. It means that even if the first two elements are distinct, it will still complete the entire search. it also uses O(k) extra space where k is the number of distinct elements in the list. – aaronasterling Oct 02 '10 at 07:58
  • Why the hell does this work faster than the naive manual iteration through all elements?? It has to build a set after all! But when I profiled this function, it worked 13 times faster than the naive implementation `for i in range(1, len(input_list)): if input_list[i-1] != input_list[i]: return False #otherwise return True` I set `input_list = ['x'] * 100000000` – max Oct 02 '10 at 08:16
  • 1
    @max. because building the set happens in C and you have a bad implementation. You should at least do it in a generator expression. See KennyTM's answer for how to do it correctly without using a set. – aaronasterling Oct 02 '10 at 08:20
  • This is the nicest, most readable answer from all here. Maybe not the most efficient, but it's not always that important. – skalee Mar 06 '13 at 21:06
  • 2
    This returns `False` if your list is empty, when it should return `True`. It also requires all your elements to be hashable. – Boris Verkhovskiy Nov 01 '20 at 17:23
  • I don't think an empty list should return True at all, False for that use case is completely expected. – jacktrader Jan 15 '22 at 18:23
24

For what it's worth, this came up on the python-ideas mailing list recently. It turns out that there is an itertools recipe for doing this already:1

def all_equal(iterable):
    "Returns True if all the elements are equal to each other"
    g = groupby(iterable)
    return next(g, True) and not next(g, False)

Supposedly it performs very nicely and has a few nice properties.

  1. Short-circuits: It will stop consuming items from the iterable as soon as it finds the first non-equal item.
  2. Doesn't require items to be hashable.
  3. It is lazy and only requires O(1) additional memory to do the check.

1In other words, I can't take the credit for coming up with the solution -- nor can I take credit for even finding it.

mgilson
  • 300,191
  • 65
  • 633
  • 696
13

This is a simple way of doing it:

result = mylist and all(mylist[0] == elem for elem in mylist)

This is slightly more complicated, it incurs function call overhead, but the semantics are more clearly spelled out:

def all_identical(seq):
    if not seq:
        # empty list is False.
        return False
    first = seq[0]
    return all(first == elem for elem in seq)
Jerub
  • 41,746
  • 15
  • 73
  • 90
  • You can avoid a redundant comparison here by using `for elem in mylist[1:]`. Doubt it improves speed much though since I guess `elem[0] is elem[0]` so the interpreter can probably do that comparison very quickly. – Brendan Jan 05 '17 at 15:58
11

This is another option, faster than len(set(x))==1 for long lists (uses short circuit)

def constantList(x):
    return x and [x[0]]*len(x) == x
6502
  • 112,025
  • 15
  • 165
  • 265
  • It is 3 times slower than the set solution on my computer, ignoring short circuit. So if the unequal element is found on average in the first third of the list, it's faster on average. – max Oct 02 '10 at 09:21
6

Check if all elements equal to the first.

np.allclose(array, array[0])

Gusev Slava
  • 2,136
  • 3
  • 21
  • 26
3

Regarding using reduce() with lambda. Here is a working code that I personally think is way nicer than some of the other answers.

reduce(lambda x, y: (x[1]==y, y), [2, 2, 2], (True, 2))

Returns a tuple where the first value is the boolean if all items are same or not.

shaik moeed
  • 5,300
  • 1
  • 18
  • 54
Marcus Lind
  • 10,374
  • 7
  • 58
  • 112
  • 1
    There is a small mistake in the code as written (try `[1, 2, 2]`): it doesn't take the previous boolean value into account. This can be fixed by replacing `x[1] == y` with `x[0] and x[1] == y`. – schot Mar 02 '20 at 08:52
3

Doubt this is the "most Pythonic", but something like:

>>> falseList = [1,2,3,4]
>>> trueList = [1, 1, 1]
>>> 
>>> def testList(list):
...   for item in list[1:]:
...     if item != list[0]:
...       return False
...   return True
... 
>>> testList(falseList)
False
>>> testList(trueList)
True

would do the trick.

machineghost
  • 33,529
  • 30
  • 159
  • 234
  • 2
    Your `for` loop can be made more Pythonic into `if any(item != list[0] for item in list[1:]): return False`, with exactly the same semantics. – musiphil Aug 18 '16 at 20:59
3

Best Answer

There was a nice Twitter thread on the various ways to implement an all_equal() function.

Given a list input, the best submission was:

 t.count(t[0]) == len(t)  

Other Approaches

Here is are the results from the thread:

  1. Have groupby() compare adjacent entries. This has an early-out for a mismatch, does not use extra memory, and it runs at C speed.

    g = itertools.groupby(s)
    next(g, True) and not next(g, False)
    
  2. Compare two slices offset from one another by one position. This uses extra memory but runs at C speed.

    s[1:] == s[:-1]
    
  3. Iterator version of slice comparison. It runs at C speed and does not use extra memory; however, the eq calls are expensive.

    all(map(operator.eq, s, itertools.islice(s, 1, None)))
    
  4. Compare the lowest and highest values. This runs at C speed, doesn't use extra memory, but does cost two inequality tests per datum.

    min(s) == max(s)  # s must be non-empty
    
  5. Build a set. This runs at C speed and uses little extra memory but requires hashability and does not have an early-out.

    len(set(t))==1.
    
  6. At great cost, this handles NaNs and other objects with exotic equality relations.

    all(itertools.starmap(eq, itertools.product(s, repeat=2)))
    
  7. Pull out the first element and compare all the others to it, stopping at the first mismatch. Only disadvantage is that this doesn't run at C speed.

     it = iter(s)
     a = next(it, None)
     return all(a == b for b in it)
    
  8. Just count the first element. This is fast, simple, elegant. It runs at C speed, requires no additional memory, uses only equality tests, and makes only a single pass over the data.

      t.count(t[0]) == len(t)
    
Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
3
def allTheSame(i):
    j = itertools.groupby(i)
    for k in j: break
    for k in j: return False
    return True

Works in Python 2.4, which doesn't have "all".

itertool
  • 31
  • 1
  • 1
    `for k in j: break` is equivalent to `next(j)`. You could also have done `def allTheSame(x): return len(list(itertools.groupby(x))<2)` if you did not care about efficiency. – ninjagecko Apr 23 '12 at 17:06
3

I'd do:

not any((x[i] != x[i+1] for i in range(0, len(x)-1)))

as any stops searching the iterable as soon as it finds a True condition.

Robert Rossney
  • 94,622
  • 24
  • 146
  • 218
  • You don't need the extra parentheses around the generator expression if it's the only argument. – ninjagecko Apr 23 '12 at 17:02
  • so does `all()`, why not use `all(x == seq[0] for x in seq)` ? looks more pythonic and should perform the same – Chen A. Sep 04 '17 at 07:36
2

You can do:

reduce(and_, (x==yourList[0] for x in yourList), True)

It is fairly annoying that python makes you import the operators like operator.and_. As of python3, you will need to also import functools.reduce.

(You should not use this method because it will not break if it finds non-equal values, but will continue examining the entire list. It is just included here as an answer for completeness.)

ninjagecko
  • 88,546
  • 24
  • 137
  • 145
2

If you're interested in something a little more readable (but of course not as efficient,) you could try:

def compare_lists(list1, list2):
    if len(list1) != len(list2): # Weed out unequal length lists.
        return False
    for item in list1:
        if item not in list2:
            return False
    return True

a_list_1 = ['apple', 'orange', 'grape', 'pear']
a_list_2 = ['pear', 'orange', 'grape', 'apple']

b_list_1 = ['apple', 'orange', 'grape', 'pear']
b_list_2 = ['apple', 'orange', 'banana', 'pear']

c_list_1 = ['apple', 'orange', 'grape']
c_list_2 = ['grape', 'orange']

print compare_lists(a_list_1, a_list_2) # Returns True
print compare_lists(b_list_1, b_list_2) # Returns False
print compare_lists(c_list_1, c_list_2) # Returns False
Joshua Burns
  • 8,268
  • 4
  • 48
  • 61
  • 1
    I'm actually trying to see if all elements in one list are identical; not if two separate lists are identical. – max Jun 05 '12 at 22:22
  • 1
    This is also enormously inefficient; for an input of length N it takes N^2 steps.**At the very least**, if the values are hashable, use a set for the containment tests. – Martijn Pieters Nov 15 '20 at 20:43
2
>>> a = [1, 2, 3, 4, 5, 6]
>>> z = [(a[x], a[x+1]) for x in range(0, len(a)-1)]
>>> z
[(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]
# Replacing it with the test
>>> z = [(a[x] == a[x+1]) for x in range(0, len(a)-1)]
>>> z
[False, False, False, False, False]
>>> if False in z : Print "All elements are not equal"
pyfunc
  • 65,343
  • 15
  • 148
  • 136
2

Or use diff method of numpy:

import numpy as np
def allthesame(l):
    return np.all(np.diff(l)==0)

And to call:

print(allthesame([1,1,1]))

Output:

True
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
1
lambda lst: reduce(lambda a,b:(b,b==a[0] and a[1]), lst, (lst[0], True))[1]

The next one will short short circuit:

all(itertools.imap(lambda i:yourlist[i]==yourlist[i+1], xrange(len(yourlist)-1)))
berdario
  • 1,851
  • 18
  • 29
  • Your first code was obviously wrong: `reduce(lambda a,b:a==b, [2,2,2])` yields `False`... I edited it, but this way it's not pretty anymore – berdario Mar 27 '14 at 09:41
1

Can use map and lambda

lst = [1,1,1,1,1,1,1,1,1]

print all(map(lambda x: x == lst[0], lst[1:]))
SuperNova
  • 25,512
  • 7
  • 93
  • 64
1

There is also a pure Python recursive option:

def checkEqual(lst):
    if len(lst)==2 :
        return lst[0]==lst[1]
    else:
        return lst[0]==lst[1] and checkEqual(lst[1:])

However for some reason it is in some cases two orders of magnitude slower than other options. Coming from C language mentality, I expected this to be faster, but it is not!

The other disadvantage is that there is recursion limit in Python which needs to be adjusted in this case. For example using this.

Tiago Martins Peres
  • 14,289
  • 18
  • 86
  • 145
Foad S. Farimani
  • 12,396
  • 15
  • 78
  • 193
1

Or use diff method of numpy:

import numpy as np
def allthesame(l):
    return np.unique(l).shape[0]<=1

And to call:

print(allthesame([1,1,1]))

Output:

True

Luis B
  • 1,684
  • 3
  • 15
  • 25
  • 2
    This answer is identical to an answer from U9-Forward from last year. – mhwombat Feb 28 '19 at 11:34
  • Good eye! I used the same structure/API, but my method uses np.unique and shape. U9's function uses np.all() and np.diff() -- I don't use either of those functions. – Luis B Mar 02 '19 at 05:59
1

The simple solution is to apply set on list

if all elements are identical len will be 1 else greater than 1

lst = [1,1,1,1,1,1,1,1,1]
len_lst = len(list(set(lst)))

print(len_lst)

1


lst = [1,2,1,1,1,1,1,1,1]
len_lst = len(list(set(lst)))
print(len_lst)

2
Uday Allu
  • 77
  • 1
  • 3
1

More versions using itertools.groupby that I find clearer than the original (more about that below):

def all_equal(iterable):
    g = groupby(iterable)
    return not any(g) or not any(g)

def all_equal(iterable):
    g = groupby(iterable)
    next(g, None)
    return not next(g, False)

def all_equal(iterable):
    g = groupby(iterable)
    return not next(g, False) or not next(g, False)

Here's the original from the Itertools Recipes again:

def all_equal(iterable):
    g = groupby(iterable)
    return next(g, True) and not next(g, False)

Note that the next(g, True) is always true (it's either a non-empty tuple or True). That means its value doesn't matter. It's executed purely for advancing the groupby iterator. But including it in the return expression leads the reader into thinking that its value gets used there. Since it doesn't, I find that misleading and unnecessarily complicated. My second version above treats the next(g, True) as what it's actually used for, as a statement whose value we don't care about.

My third version goes a different direction and does use the value of the first next(g, False). If there isn't even a first group at all (i.e., if the given iterable is "empty"), then that solution returns the result right away and doesn't even check whether there's a second group.

My first solution is basically the same as my third, just using any. Both solutions read as "All elements are equal iff ... there is no first group or there is no second group."

Benchmark results (although speed is really not my point here, clarity is, and in practice if there are many equal values, most of the time might be spent by the groupby itself, reducing the impact of these differences here):

Python 3.10.4 on my Windows laptop:

iterable = ()
 914 ns   914 ns   916 ns  use_first_any
 917 ns   925 ns   925 ns  use_first_next
1074 ns  1075 ns  1075 ns  next_as_statement
1081 ns  1083 ns  1084 ns  original

iterable = (1,)
1290 ns  1290 ns  1291 ns  next_as_statement
1303 ns  1307 ns  1307 ns  use_first_next
1306 ns  1307 ns  1309 ns  use_first_any
1318 ns  1319 ns  1320 ns  original

iterable = (1, 2)
1463 ns  1464 ns  1467 ns  use_first_any
1463 ns  1463 ns  1467 ns  next_as_statement
1477 ns  1479 ns  1481 ns  use_first_next
1487 ns  1489 ns  1492 ns  original
Python 3.10.4 on a Debian Google Compute Engine instance:

iterable = ()
 234 ns   234 ns   234 ns  use_first_any
 234 ns   235 ns   235 ns  use_first_next
 264 ns   264 ns   264 ns  next_as_statement
 265 ns   265 ns   265 ns  original

iterable = (1,)
 308 ns   308 ns   308 ns  next_as_statement
 315 ns   315 ns   315 ns  original
 316 ns   316 ns   317 ns  use_first_any
 317 ns   317 ns   317 ns  use_first_next

iterable = (1, 2)
 361 ns   361 ns   361 ns  next_as_statement
 367 ns   367 ns   367 ns  original
 384 ns   385 ns   385 ns  use_first_next
 386 ns   387 ns   387 ns  use_first_any

Benchmark code:

from timeit import timeit
from random import shuffle
from bisect import insort
from itertools import groupby

def original(iterable):
    g = groupby(iterable)
    return next(g, True) and not next(g, False)

def use_first_any(iterable):
    g = groupby(iterable)
    return not any(g) or not any(g)

def next_as_statement(iterable):
    g = groupby(iterable)
    next(g, None)
    return not next(g, False)

def use_first_next(iterable):
    g = groupby(iterable)
    return not next(g, False) or not next(g, False)

funcs = [original, use_first_any, next_as_statement, use_first_next]

for iterable in (), (1,), (1, 2):
    print(f'{iterable = }')
    times = {func: [] for func in funcs}
    for _ in range(1000):
        shuffle(funcs)
        for func in funcs:
            number = 1000
            t = timeit(lambda: func(iterable), number=number) / number
            insort(times[func], t)
    for func in sorted(funcs, key=times.get):
        print(*('%4d ns ' % round(t * 1e9) for t in times[func][:3]), func.__name__)
    print()
Michel de Ruiter
  • 7,131
  • 5
  • 49
  • 74
Stefan Pochmann
  • 27,593
  • 8
  • 44
  • 107
0

You can use .nunique() to find number of unique items in a list.

def identical_elements(list):
    series = pd.Series(list)
    if series.nunique() == 1: identical = True
    else:  identical = False
    return identical



identical_elements(['a', 'a'])
Out[427]: True

identical_elements(['a', 'b'])
Out[428]: False
Saeed
  • 1,848
  • 1
  • 18
  • 26
0

Maybe I'm underestimating the problem? Check the length of unique values in the list.

lzt = [1,1,1,1,1,2]

if (len(set(lzt)) > 1):
    uniform = False
elif (len(set(lzt)) == 1):
    uniform = True
elif (not lzt):
    raise ValueError("List empty, get wrecked")
Kermit
  • 4,922
  • 4
  • 42
  • 74
0

I ended up with this one-liner

from itertools import starmap, pairwise
all(starmap(eq, (pairwise(x)))
Lajos
  • 2,549
  • 6
  • 31
  • 38
-1

This was a fun one to read through and think about. Thanks everyone! I don't think anything relying on pure count will be reliable for all cases. Also sum could work but only for numbers or length (again resulting in a count scenario).

But I do like the simplicity, so this is what I came up with:

all(i==lst[c-1] for c, i in enumerate(lst))

Alternatively, I do think this clever one by @kennytm would also work for all cases (and is probably the fastest, interestingly). So I concede that it's probably better than mine:

[lst[0]]*len(lst) == lst

A little bonus clever one I think would also work because set gets rid of duplicates (and clever is fun but not generally best practice for maintaining code). And I think the one by @kennytm would still be faster but really only relevant for large lists:

len(set(lst)) == 1

But the simplicity and cleverness of Python is one of my favorite things about the language. And thinking about it a little more, if you have to modify the list in anyway, like I actually do because I'm comparing addresses (and will remove leading/trailing spaces and convert to lower case to remove possible inconsistencies, mine would be more suited for the job). So "better" is subjective as I eluded to by using quotes when I use that word! But you could also cleanup the list beforehand.

Best and good luck!

jacktrader
  • 588
  • 5
  • 10
  • Good catch, I glanced at the command and obviously hadn't thought it through all the way.. It splits the list and then reverses to compare (I had only saw the reverse). So it might work for all cases, I'm not sure? But I just removed it until I can test some use cases. But the logic is also hard to follow for large cases, so I'm not sure I'd use it anyways. Thanks! – jacktrader Jan 16 '22 at 18:39
  • `-1`. These deliberations do not add much to, for example, @ninjagecko's answer from way back in 2012: `all(x==myList[0] for x in myList)`. The top proposed solution here is similar but less performant and harder to comprehend. – Hans Bouwmeester Mar 06 '22 at 18:01
-1

I suggest a simple pythonic solution:

def all_equal_in_iterable(iterable: Iterable):
    iterable = list(iterable)
    if not iterable:
        return False
    return all(item == iterable[0] for item in iterable)
Luca Di Liello
  • 1,486
  • 2
  • 17
  • 34
-2

Here is a code with good amount of Pythonicity, and balance of simplicity and obviousness, I think, which should work also in pretty old Python versions.

def all_eq(lst):
    for idx, itm in enumerate(lst):
        if not idx:   # == 0
            prev = itm
        if itm != prev:
            return False
        prev = itm
    return True
mykhal
  • 19,175
  • 11
  • 72
  • 80