From: Floris Bruynooghe on
One thing I ofter wonder is which is better when you just need a
throwaway sequence: a list or a tuple? E.g.:

if foo in ['some', 'random', 'strings']:
...
if [bool1, bool2, boo3].count(True) != 1:
...

(The last one only works with tuples since python 2.6)

Is a list or tuple better or more efficient in these situations?


Regards
Floris

PS: This is inspired by some of the space-efficiency comments from the
list.pop(0) discussion.
From: Iain King on
On Jan 27, 10:20 am, Floris Bruynooghe <floris.bruynoo...(a)gmail.com>
wrote:
> One thing I ofter wonder is which is better when you just need a
> throwaway sequence: a list or a tuple?  E.g.:
>
> if foo in ['some', 'random', 'strings']:
>     ...
> if [bool1, bool2, boo3].count(True) != 1:
>    ...
>
> (The last one only works with tuples since python 2.6)
>
> Is a list or tuple better or more efficient in these situations?
>
> Regards
> Floris
>
> PS: This is inspired by some of the space-efficiency comments from the
> list.pop(0) discussion.

I tend to use tuples unless using a list makes it easier to read. For
example:

if foo in ('some', 'random', 'strings'):

draw.text((10,30), "WHICH IS WHITE", font=font)
draw.line([(70,25), (85,25), (105,45)])

I've no idea what the performance difference is; I've always assumed
it's negligible.

Iain
From: M.-A. Lemburg on
Iain King wrote:
> On Jan 27, 10:20 am, Floris Bruynooghe <floris.bruynoo...(a)gmail.com>
> wrote:
>> One thing I ofter wonder is which is better when you just need a
>> throwaway sequence: a list or a tuple? E.g.:
>>
>> if foo in ['some', 'random', 'strings']:
>> ...
>> if [bool1, bool2, boo3].count(True) != 1:
>> ...
>>
>> (The last one only works with tuples since python 2.6)
>>
>> Is a list or tuple better or more efficient in these situations?
>>
>> Regards
>> Floris
>>
>> PS: This is inspired by some of the space-efficiency comments from the
>> list.pop(0) discussion.
>
> I tend to use tuples unless using a list makes it easier to read. For
> example:
>
> if foo in ('some', 'random', 'strings'):
>
> draw.text((10,30), "WHICH IS WHITE", font=font)
> draw.line([(70,25), (85,25), (105,45)])
>
> I've no idea what the performance difference is; I've always assumed
> it's negligible.

Tuples are a faster to create than lists, so if you create
lots of them and don't need the list features, use tuples.

--
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source (#1, Jan 27 2010)
>>> Python/Zope Consulting and Support ... http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
________________________________________________________________________

::: Try our new mxODBC.Connect Python Database Interface for free ! ::::


eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
http://www.egenix.com/company/contact/
From: Steve Holden on
Iain King wrote:
> On Jan 27, 10:20 am, Floris Bruynooghe <floris.bruynoo...(a)gmail.com>
> wrote:
>> One thing I ofter wonder is which is better when you just need a
>> throwaway sequence: a list or a tuple? E.g.:
>>
>> if foo in ['some', 'random', 'strings']:
>> ...
>> if [bool1, bool2, boo3].count(True) != 1:
>> ...
>>
>> (The last one only works with tuples since python 2.6)
>>
>> Is a list or tuple better or more efficient in these situations?
>>
>> Regards
>> Floris
>>
>> PS: This is inspired by some of the space-efficiency comments from the
>> list.pop(0) discussion.
>
> I tend to use tuples unless using a list makes it easier to read. For
> example:
>
> if foo in ('some', 'random', 'strings'):
>
> draw.text((10,30), "WHICH IS WHITE", font=font)
> draw.line([(70,25), (85,25), (105,45)])
>
> I've no idea what the performance difference is; I've always assumed
> it's negligible.
>
The fact that you have felt able to neglect the performance difference
makes it quite literally negligible.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/
From: Antoine Pitrou on
Le Wed, 27 Jan 2010 02:20:53 -0800, Floris Bruynooghe a écrit :
>
> Is a list or tuple better or more efficient in these situations?

Tuples are faster to allocate (they are allocated in one single step) and
quite a bit smaller too.
In some situations, in Python 2.7 and 3.1, they can also be ignored by
the garbage collector, yielding faster collections.

(not to mention that they are hashable, which can be useful)


 |  Next  |  Last
Pages: 1 2
Prev: Python or Ant
Next: myths about python 3