Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1106451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:47:42+00:00 2026-05-17T01:47:42+00:00

I need a function which takes in a list and outputs True if all

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T01:47:42+00:00Added an answer on May 17, 2026 at 1:47 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i need to write a function, which takes positive integers list. If list begins
I need a function which takes an arbitrary number of arguments (All of the
I need some help in getting this right, problem Write a function which takes
I need a function which returns true if the certificate of a secure website
I need to create a function that can takes a list of tuples and
I have the following problem: I have a function which takes a List[Double] as
I need a function which will check for the existing URLs in a string.
I need to create a function which rounds decimal numbers like this: Round($32.95, 0)
I need to write a TSQL user defined function which will accept a string
I need to implement a simp^le find function which will retrieve the 1st occurence

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.