Re: Are all items in list the same?

2019-01-08 Thread Karen Shaeffer
There was one more error. (smiles ;) Fixed below def all_equal_array_list(alist) -> bool: if alist[1:] == alist[:-1]: return True return False def all_equal(alist) -> bool: if len(alist) == 0 or all(i == alist[0] for i in alist[1:]): return True return False

Re: Are all items in list the same?

2019-01-08 Thread Karen Shaeffer
There were some issues with my test. After sending the email, I thought those times couldn't be real. Here are better results: all_equal(tlst) times = [6.719925760501064e-05] seconds. all_equal_array_list(tlst) times = [4.2184268278069794e-06] seconds. import timeit def all_equal(alist)

Re: Are all items in list the same?

2019-01-08 Thread Karen Shaeffer
On 08Jan2019 15:28, Dan Sommers <2qdxy4rzwzuui...@potatochowder.com> wrote: >>>>> a = [1, 1, 1, 1, 1] >>>>> a[1:] == a[:-1] >>True >>>>> a == a[::-1] >>True >> >>>>> a = [1, 2, 3, 4, 3, 2, 1] >>>>> a[1:] == a[:-1] >>False >>>>> a == a[::-1] >>True

Re: Are all items in list the same?

2019-01-08 Thread Cameron Simpson
On 08Jan2019 15:28, Dan Sommers <2qdxy4rzwzuui...@potatochowder.com> wrote: >>> a = [1, 1, 1, 1, 1] >>> a[1:] == a[:-1] True >>> a == a[::-1] True >>> a = [1, 2, 3, 4, 3, 2, 1] >>> a[1:] == a[:-1] False >>> a == a[::-1] True Looks like Peter's pretty clever after

Re: Are all items in list the same?

2019-01-08 Thread Dan Sommers
On 1/8/19 2:31 PM, Tobiah wrote:> On 1/8/19 9:20 AM, Alister wrote: >> On Tue, 08 Jan 2019 17:15:17 +, Alister wrote: >> >>> On Tue, 08 Jan 2019 16:48:58 +0200, Serhiy Storchaka wrote: >>> 08.01.19 11:07, Peter Otten пише: > Bob van der Poel wrote: > >> I need to see if all

Re: Are all items in list the same?

2019-01-08 Thread Tobiah
On 1/8/19 9:20 AM, Alister wrote: On Tue, 08 Jan 2019 17:15:17 +, Alister wrote: On Tue, 08 Jan 2019 16:48:58 +0200, Serhiy Storchaka wrote: 08.01.19 11:07, Peter Otten пише: Bob van der Poel wrote: I need to see if all the items in my list are the same. I was using set() for this,

Re: Are all items in list the same?

2019-01-08 Thread ike
On Tue, Jan 08, 2019 at 01:05:09PM +1100, Chris Angelico wrote: > No, because the iteration would never happen. If the list is empty, > a[1:] is also empty, and i==a[0] will never be evaluated. So it is > safe. (But I agree that it's not instantly obvious.) Oh yeah, you're right. I overthought it

Re: Are all items in list the same?

2019-01-08 Thread Alister via Python-list
On Tue, 08 Jan 2019 17:15:17 +, Alister wrote: > On Tue, 08 Jan 2019 16:48:58 +0200, Serhiy Storchaka wrote: > >> 08.01.19 11:07, Peter Otten пише: >>> Bob van der Poel wrote: >>> I need to see if all the items in my list are the same. I was using set() for this, but that

Re: Are all items in list the same?

2019-01-08 Thread Alister via Python-list
On Tue, 08 Jan 2019 16:48:58 +0200, Serhiy Storchaka wrote: > 08.01.19 11:07, Peter Otten пише: >> Bob van der Poel wrote: >> >>> I need to see if all the items in my list are the same. I was using >>> set() >>> for this, but that doesn't work if items are themselves lists. So, >>> assuming that

Re: Are all items in list the same?

2019-01-08 Thread Bob van der Poel
Thanks guys for the help on this. As it turns out I have learned new commands as a result of the question: all() and any() will work perfectly! The other solutions, as always, are enlightening. And, no, Chris, this is not homework :) On Tue, Jan 8, 2019 at 9:31 AM Neil Cerutti wrote: > On

Re: Are all items in list the same?

2019-01-08 Thread Neil Cerutti
On 2019-01-08, MRAB wrote: > On 2019-01-08 00:47, i...@koeln.ccc.de wrote: >> You might do something like >> >> if len(a) == 0 or all(i == a[0] for i in a[1:]): >> > You don't need to check the length of the list because if the list is > empty, 'all' will return True anyway. Neat! I

Re: Are all items in list the same?

2019-01-08 Thread Serhiy Storchaka
08.01.19 11:07, Peter Otten пише: Bob van der Poel wrote: I need to see if all the items in my list are the same. I was using set() for this, but that doesn't work if items are themselves lists. So, assuming that a is a list of some things, the best I've been able to come up with it: if

Re: Are all items in list the same?

2019-01-08 Thread Peter Otten
Bob van der Poel wrote: > I need to see if all the items in my list are the same. I was using set() > for this, but that doesn't work if items are themselves lists. So, > assuming that a is a list of some things, the best I've been able to come > up with it: > > if a.count( targ ) == len(a):

Re: Are all items in list the same?

2019-01-08 Thread Peter Otten
Tim Chase wrote: > def all_equal(iterable): > i = iter(iterable) > first = next(i) > return all(x == first for x in i) > > It's undefined for an empty list (well, it throws a StopIteration > but you can special-case that), but should hand the cases with > 1 element and 2+ elements

Re: Are all items in list the same?

2019-01-07 Thread Chris Angelico
On Tue, Jan 8, 2019 at 1:03 PM Skip Montanaro wrote: > > > > > > if len(a) == 0 or all(i == a[0] for i in a[1:]): > > > > > You don't need to check the length of the list because if the list is > > empty, 'all' will return True anyway. > > > > Given the structure of the expression passed as

Re: Are all items in list the same?

2019-01-07 Thread Skip Montanaro
> > > if len(a) == 0 or all(i == a[0] for i in a[1:]): > > > You don't need to check the length of the list because if the list is > empty, 'all' will return True anyway. > Given the structure of the expression passed as an argument to all(), won't you get an IndexError if a is empty without

Re: Are all items in list the same?

2019-01-07 Thread MRAB
On 2019-01-08 00:47, i...@koeln.ccc.de wrote: You might do something like if len(a) == 0 or all(i == a[0] for i in a[1:]): You don't need to check the length of the list because if the list is empty, 'all' will return True anyway. This should be linear complexity and short circuiting

Re: Are all items in list the same?

2019-01-07 Thread Chris Angelico
On Tue, Jan 8, 2019 at 12:26 PM Tim Chase wrote: > def all_equal(iterable): > i = iter(iterable) > first = next(i) > return all(x == first for x in i) > > And I even like how nicely it reads :-) Yes, there's something beautiful about writing "first = next" :-) ChrisA --

Re: Are all items in list the same?

2019-01-07 Thread Tim Chase
On 2019-01-07 17:14, Bob van der Poel wrote: > I need to see if all the items in my list are the same. I was using > set() for this, but that doesn't work if items are themselves > lists. So, assuming that a is a list of some things, the best I've > been able to come up with it: > > if

Re: Are all items in list the same?

2019-01-07 Thread Cameron Simpson
On 07Jan2019 17:14, bvdp wrote: I need to see if all the items in my list are the same. I was using set() for this, but that doesn't work if items are themselves lists. So, assuming that a is a list of some things, the best I've been able to come up with it: if a.count( targ ) == len(a):

Re: Are all items in list the same?

2019-01-07 Thread ike
You might do something like if len(a) == 0 or all(i == a[0] for i in a[1:]): This should be linear complexity and short circuiting and in general it doesn't get much better than this. Though I wouldn't bet there isn't a better (faster/clearer/more readable) solution. On Mon, Jan 07, 2019 at

Re: Are all items in list the same?

2019-01-07 Thread MRAB
On 2019-01-08 00:14, Bob van der Poel wrote: I need to see if all the items in my list are the same. I was using set() for this, but that doesn't work if items are themselves lists. So, assuming that a is a list of some things, the best I've been able to come up with it: if a.count( targ )

Are all items in list the same?

2019-01-07 Thread Bob van der Poel
I need to see if all the items in my list are the same. I was using set() for this, but that doesn't work if items are themselves lists. So, assuming that a is a list of some things, the best I've been able to come up with it: if a.count( targ ) == len(a): I'm somewhat afraid that this won't