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 8927557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:14:42+00:00 2026-06-15T08:14:42+00:00

I am looking for a better way, may be using list comprehensions? >>> x

  • 0

I am looking for a better way, may be using list comprehensions?

>>> x = [1, 1, 1, 1, 1, 1]
>>> x
[1, 1, 1, 1, 1, 1]
>>> for i in x:
...   if i!=1:
...     print "fail"
... 
>>> 
>>> x = [1, 1, 1, 1, 1, 0]
>>> for i in x:
...   if i!=1:
...     print "fail"
... 
fail
>>> 
  • 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-06-15T08:14:43+00:00Added an answer on June 15, 2026 at 8:14 am
    >>> x = [1, 1, 1, 1, 1, 1]
    >>> all(el==1 for el in x)
    True
    

    This uses the function all with a generator expression.


    If you always have only zeroes and ones in the list (or if you want to just check if the list doesn’t have any zeroes), then just use all without any additional tricks:

    >>> x = [1, 0, 1, 1, 1, 0]
    >>> all(x)
    False
    

    Benchmark of some solutions:
    The numbers mean what time in milliseconds it took to run the solution once (average of 1000 timeit runs)

    Python 3.2.3

                  all(el==1 for el in x): 0.0003 0.0008 0.7903 0.0804 0.0005 0.0006
                           x==[1]*len(x): 0.0002 0.0003 0.0714 0.0086 0.0045 0.0554
             not([1 for y in x if y!=1]): 0.0003 0.0005 0.4142 0.1117 0.1100 1.1630
                    set(x).issubset({1}): 0.0003 0.0005 0.2039 0.0409 0.0476 0.5310
    y = set(x); len(y)==1 and y.pop()==1:   WA   0.0006 0.2043 0.0517 0.0409 0.4170
                       max(x)==1==min(x):   RE   0.0006 0.4574 0.0460 0.0917 0.5466
                     tuple(set(x))==(1,):   WA   0.0006 0.2046 0.0410 0.0408 0.4238
    not(bool(filter(lambda y: y!=1, x))):   WA     WA     WA   0.0004 0.0004 0.0004
                                  all(x): 0.0001 0.0001 0.0839   WA   0.0001   WA  
    

    Python 2.7.3

                  all(el==1 for el in x): 0.0003 0.0008 0.7175 0.0751 0.0006 0.0006
                           x==[1]*len(x): 0.0002 0.0003 0.0741 0.0110 0.0094 0.1015
             not([1 for y in x if y!=1]): 0.0001 0.0003 0.3908 0.0948 0.0954 0.9840
                    set(x).issubset({1}): 0.0003 0.0005 0.2084 0.0422 0.0420 0.4198
    y = set(x); len(y)==1 and y.pop()==1:   WA   0.0006 0.2083 0.0421 0.0418 0.4178
                       max(x)==1==min(x):   RE   0.0006 0.4568 0.0442 0.0866 0.4937
                     tuple(set(x))==(1,):   WA   0.0006 0.2086 0.0424 0.0421 0.4202
    not(bool(filter(lambda y: y!=1, x))): 0.0004 0.0011 0.9809 0.1936 0.1925 2.0007
                                  all(x): 0.0001 0.0001 0.0811   WA   0.0001   WA  
    

    [PyPy 1.9.0] Python 2.7.2

                  all(el==1 for el in x): 0.0013 0.0093 0.4148 0.0508 0.0036 0.0038
                           x==[1]*len(x): 0.0006 0.0009 0.4557 0.0575 0.0177 0.1368
             not([1 for y in x if y!=1]): 0.0009 0.0015 175.10 7.0742 6.4390 714.15 # No, this wasn't run 1000 times. Had to time it separately.
                    set(x).issubset({1}): 0.0010 0.0020 0.0657 0.0138 0.0139 0.1303
    y = set(x); len(y)==1 and y.pop()==1:   WA   0.0011 0.0651 0.0137 0.0137 0.1296
                       max(x)==1==min(x):   RE   0.0011 0.5892 0.0615 0.1171 0.5994
                     tuple(set(x))==(1,):   WA   0.0014 0.0656 0.0163 0.0142 0.1302
    not(bool(filter(lambda y: y!=1, x))): 0.0030 0.0081 0.2171 0.0689 0.0680 0.7599
                                  all(x): 0.0011 0.0044 0.0230   WA   0.0013   WA  
    

    The following test cases were used:

    [] # True
    [1]*6 # True
    [1]*10000 # True
    [1]*1000+[2]*1000 # False
    [0]*1000+[1]*1000 # False
    [random.randint(1, 2) for _ in range(20000)] # False
    

    WA means that the solution gave a wrong answer; RE stands for runtime error.


    So my verdict is, Winston Ewert‘s x==[1]*len(x) solution is the fastest in most cases. If you rarely have lists of all ones (the data is random, etc.) or you don’t want to use additional RAM, my solution works better. If the lists are small, the difference is negligible.

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

Sidebar

Related Questions

I am looking to provide a better way of managing the distribution and storage
Is there any better way to make this query work? I'm looking for a
Looking on SO, I see that the preferred way to currency using RoR is
I am looking for a better primary key than the autonumber data type, namely
I was looking for a better mechanism for notifying my desktop clients that a
I'm looking for a better solution for two things: How can I understand if
Alex explained what I'm looking for much better than I have: You want an
I'm trying to learn Rails better by looking at example applications, and while looking
In asking this question, I'm looking for either better understanding of the situation or
I'm looking for a framework that is better and easier to use than Apache

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.