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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thorsten Kampe

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

    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
  • Peter Hansen

    #2
    Re: suggestions for using tuples instead of list (or vice versa)

    Thorsten Kampe wrote:
    [color=blue]
    > 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.[/color]

    Only tuples can be used as dictionary keys.
    [color=blue]
    > Is there any advantage for using tuples? Are they "faster"? Consume
    > less memory?[/color]

    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.
    [color=blue]
    > When is it better to use tuples instead of lists and when
    > better to use lists instead of tuples?[/color]

    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

    Comment

    • Diez B. Roggisch

      #3
      Re: suggestions for using tuples instead of list (or vice versa)

      > Is there any advantage for using tuples? Are they "faster"? Consume[color=blue]
      > less memory? When is it better to use tuples instead of lists and when
      > better to use lists instead of tuples?[/color]

      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

      Comment

      • Roy Smith

        #4
        Re: suggestions for using tuples instead of list (or vice versa)

        In article <ylxi7lokk9ms$. dlg@thorstenkam pe.de>,
        Thorsten Kampe <thorsten@thors tenkampe.de> wrote:
        [color=blue]
        > I found out that I am rarely using tuples and almost always lists
        > because of the more flexible usability of lists (methods, etc.)[/color]

        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.

        Comment

        Working...