suggestions for using tuples instead of list (or vice versa)

Discussion in 'Python' started by Thorsten Kampe, Apr 29, 2004.

  1. I found out that I am rarely using tuples and almost always lists
    because of the more flexible usability of lists (methods, etc.)

    To my knowledge, the only fundamental difference between tuples and
    lists is that tuples are immutable, so if this is correct, than list
    are a superset of tuples, meaning lists can do everything tuples can
    do and more.

    Is there any advantage for using tuples? Are they "faster"? Consume
    less memory? When is it better to use tuples instead of lists and when
    better to use lists instead of tuples?

    Thorsten
     
    Thorsten Kampe, Apr 29, 2004
    #1
    1. Advertising

  2. > Is there any advantage for using tuples? Are they "faster"? Consume
    > less memory? When is it better to use tuples instead of lists and when
    > better to use lists instead of tuples?


    AFAIK tuples are faster and less memory consuming. I personally prefer them
    for e.g. returning multiple values from a function or when I know I won't
    need the power of a list. But its largely considered a matter of taste.
    Google this newsgroup for a plethora of discussions on this subject....

    --
    Regards,

    Diez B. Roggisch
     
    Diez B. Roggisch, Apr 29, 2004
    #2
    1. Advertising

  3. Thorsten Kampe

    Peter Hansen Guest

    Thorsten Kampe wrote:

    > I found out that I am rarely using tuples and almost always lists
    > because of the more flexible usability of lists (methods, etc.)
    >
    > To my knowledge, the only fundamental difference between tuples and
    > lists is that tuples are immutable, so if this is correct, than list
    > are a superset of tuples, meaning lists can do everything tuples can
    > do and more.


    Only tuples can be used as dictionary keys.

    > Is there any advantage for using tuples? Are they "faster"? Consume
    > less memory?


    Not appreciably so in most cases. Certainly not enough to
    be a factor in choosing which to use except perhaps in
    very rare edge cases.

    > When is it better to use tuples instead of lists and when
    > better to use lists instead of tuples?


    The canonical answer (which some disagree with) is that you
    should use tuples when you have a collection of different
    types of items, such as a 'struct' in C would have. Use
    lists any other time you want a simple sequential collection
    of items.

    Use a list for effectively everything else, unless you need
    an immutable (tuple) for a dictionary key.

    One description of how to look at this that I've found helpful
    is something along the lines of "if you can take a slice of
    the data and consider it in the same way as the original,
    then you should use a list". For example, if you have a set
    of fifteen objects over which you plan to iterate, you can
    just as well iterate over the first five. If, however, you
    have a tuple of (year, month, day, hour, minute, second),
    taking the first four items is quite a different thing than
    taking all six of them.

    -Peter
     
    Peter Hansen, Apr 29, 2004
    #3
  4. Thorsten Kampe

    Roy Smith Guest

    In article <ylxi7lokk9ms$>,
    Thorsten Kampe <> wrote:

    > I found out that I am rarely using tuples and almost always lists
    > because of the more flexible usability of lists (methods, etc.)


    There was an extensive thread on this topic within the last month or
    two. Like many threads, it rambled and wandered a bit, but covered the
    topic quite well. DAGS for list or tuple in the subject, date <= 2
    months, and you should find it.
     
    Roy Smith, Apr 29, 2004
    #4
    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. Jimmy
    Replies:
    1
    Views:
    3,161
    Eliyahu Goldin
    Jun 14, 2005
  2. GaryM
    Replies:
    1
    Views:
    347
    Christophe Vanfleteren
    Jan 8, 2004
  3. Jabba Laci

    list to tuple and vice versa

    Jabba Laci, Oct 18, 2009, in forum: Python
    Replies:
    0
    Views:
    299
    Jabba Laci
    Oct 18, 2009
  4. Jabba Laci

    Re: list to tuple and vice versa

    Jabba Laci, Oct 18, 2009, in forum: Python
    Replies:
    2
    Views:
    312
    StarWing
    Oct 18, 2009
  5. Jon Reyes
    Replies:
    18
    Views:
    232
    Mitya Sirenef
    Feb 19, 2013
Loading...

Share This Page