Ad hoc lists vs ad hoc tuples

Discussion in 'Python' started by Floris Bruynooghe, Jan 27, 2010.

  1. 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.
    Floris Bruynooghe, Jan 27, 2010
    #1
    1. Advertising

  2. Floris Bruynooghe

    Iain King Guest

    On Jan 27, 10:20 am, Floris Bruynooghe <>
    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
    Iain King, Jan 27, 2010
    #2
    1. Advertising

  3. Floris Bruynooghe

    Steve Holden Guest

    Iain King wrote:
    > On Jan 27, 10:20 am, Floris Bruynooghe <>
    > 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/
    Steve Holden, Jan 27, 2010
    #3
  4. Floris Bruynooghe

    Steve Holden Guest

    Iain King wrote:
    > On Jan 27, 10:20 am, Floris Bruynooghe <>
    > 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/
    Steve Holden, Jan 27, 2010
    #4
  5. 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)
    Antoine Pitrou, Jan 27, 2010
    #5
  6. Floris Bruynooghe

    Terry Reedy Guest

    On 1/27/2010 12:32 PM, Antoine Pitrou wrote:
    > 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)


    Constant tuples (a tuple whose members are all seen as constants by the
    compiler) are now pre-compiled and constructed once and put into the
    code object as such rather than re-constructed with each run of the code.

    >>> from dis import dis
    >>> def l(): return [1,2,3]


    >>> def t(): return 1,2,3


    >>> dis(l)

    1 0 LOAD_CONST 1 (1)
    3 LOAD_CONST 2 (2)
    6 LOAD_CONST 3 (3)
    9 BUILD_LIST 3
    12 RETURN_VALUE
    >>> dis(t)

    1 0 LOAD_CONST 4 ((1, 2, 3))
    3 RETURN_VALUE
    >>> # 3.1


    Terry Jan Reedy
    Terry Reedy, Jan 27, 2010
    #6
  7. On Jan 27, 10:15 pm, Terry Reedy <> wrote:
    > On 1/27/2010 12:32 PM, Antoine Pitrou wrote:
    >
    > > 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.


    Thanks for all the answers! This is what I was expecting but it's
    nice to see it confirmed.

    Regards
    Floris
    Floris Bruynooghe, Jan 28, 2010
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daniel Nogradi
    Replies:
    3
    Views:
    349
    Dennis Lee Bieber
    Nov 10, 2006
  2. Replies:
    5
    Views:
    552
    Thomas J. Gritzan
    Oct 6, 2006
  3. tuples within tuples

    , Oct 26, 2007, in forum: Python
    Replies:
    12
    Views:
    572
    Dennis Lee Bieber
    Oct 27, 2007
  4. xera121
    Replies:
    8
    Views:
    721
    lolmc
    Sep 30, 2009
  5. Jon Reyes
    Replies:
    18
    Views:
    228
    Mitya Sirenef
    Feb 19, 2013
Loading...

Share This Page