what is the difference between tuple and list?

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

    #1

    what is the difference between tuple and list?

    is there any typical usage that shows their difference?

    thanks

    daniel

  • danmcleran@yahoo.com

    #2
    Re: what is the difference between tuple and list?

    Lists are mutable, i.e. one can do this:

    a = [1,2,3]

    a[0] = 100

    You can't do that with a tuple.

    a = (1,2,3)

    a[0] = 100 # error

    Comment

    • Diez B. Roggisch

      #3
      Re: what is the difference between tuple and list?

      daniel wrote:
      [color=blue]
      > is there any typical usage that shows their difference?[/color]
      [color=blue][color=green][color=darkred]
      >>> d = {}
      >>> d[('multi', 'part', 'key')] = 'schnarz'
      >>> d[['multi', 'part', 'key']] = 'schnarz'[/color][/color][/color]
      Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      TypeError: list objects are unhashable


      A lot of discussions regarding this have been fiercly fought on this list -
      go google :)

      Diez

      Comment

      • infidel

        #4
        Re: what is the difference between tuple and list?

        > is there any typical usage that shows their difference?

        I think the general idea is to use lists for homogenous collections and
        tuples for heterogenous structures.

        I think the database API provides a good usage that shows their
        differences. When you do cursor.fetchall () after executing a query,
        you get back a list of tuples. Each tuple is one "record" from the
        cursor. "tuple" is even the term used in relational database theory
        when talking about a table row. You shouldn't be able to add or remove
        fields from a query result, so using a tuple is a perfect match for
        this. On the other hand, you can certainly add, delete, or replace
        entire tuples in the result set, so it makes sense to use a list to
        hold the set of tuples.

        Comment

        • Harold Fellermann

          #5
          Re: what is the difference between tuple and list?

          The main difference is that lists are mutables while tuples are not.

          Tuples are fine if you only want to group some objects (e.g. as a
          return value) and access their members as in[color=blue][color=green][color=darkred]
          >>> t = (1,2,3,4)
          >>> t[2][/color][/color][/color]
          3

          Lists give you a lot more flexibility, because they are mutable: you
          can change the order of elements (e.g. sort), or delete or append items
          (all that this is not possible for tuples). These features make lists
          the primary data structures for stacks, queues, a.s.o.

          So, whenever you want to change what objects are in your collection or
          their ordering, use lists, otherwise, use tuples. Note, that this does
          not mean, that the items themselves cannot be changed. You can
          perfectly well change an object (e.g. dictionary) that resides in a
          tuple:
          [color=blue][color=green][color=darkred]
          >>> t = ({},) # tuple with empty dict as its only item
          >>> t[0]["foo"] = "bar"[/color][/color][/color]

          But if you e.g. want to exchange, that dictionary by another, you need
          to go for a list instead of a tuple.

          Hope that made sense...

          - harold -

          Comment

          • Simon Brunning

            #6
            Re: what is the difference between tuple and list?

            On 16 May 2006 07:47:24 -0700, daniel <daniel.huangfe i@gmail.com> wrote:

            <http://www.python.org/doc/faq/general.html#wh y-are-there-separate-tuple-and-list-data-types>

            --
            Cheers,
            Simon B,
            simon@brunningo nline.net,

            Comment

            • daniel

              #7
              Re: what is the difference between tuple and list?

              thank you all for replying, I'm new to python, and just reading the
              python tutorial now. I did not expect the FAQ to contain any relevant
              topics, so thanks Simon...

              your comments did make sense, I should have read the tutorial more
              thoroughly, It's not a good question, I admit. ;-)

              English is not my mother language, so sorry if there is any mistakes or
              improper expression.

              Comment

              • bruno at modulix

                #8
                Re: what is the difference between tuple and list?

                infidel wrote:[color=blue][color=green]
                >>is there any typical usage that shows their difference?[/color]
                >
                >
                > I think the general idea is to use lists for homogenous collections and
                > tuples for heterogenous structures.
                >
                > I think the database API provides a good usage that shows their
                > differences. When you do cursor.fetchall () after executing a query,
                > you get back a list of tuples. Each tuple is one "record" from the
                > cursor. "tuple" is even the term used in relational database theory
                > when talking about a table row. You shouldn't be able to add or remove
                > fields from a query result, so using a tuple is a perfect match for
                > this. On the other hand, you can certainly add, delete, or replace
                > entire tuples in the result set, so it makes sense to use a list to
                > hold the set of tuples.
                >[/color]

                I think you can remove the 'I think' !-)

                --
                bruno desthuilliers
                python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
                p in 'onurb@xiludom. gro'.split('@')])"

                Comment

                Working...