Tuple vs List: Whats the difference?

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

    Tuple vs List: Whats the difference?

    Hello folks,

    I am an experienced programmer, but very new to python (2 days). I
    wanted to ask: what exactly is the difference between a tuple and a
    list? I'm sure there are some, but I can't seem to find a situation
    where I can use one but not the other.

    Thanks in advance,
    --Shafik

  • Joe Riopel

    #2
    Re: Tuple vs List: Whats the difference?

    On 7/11/07, Shafik <shafik23@gmail .comwrote:
    I am an experienced programmer, but very new to python (2 days). I
    wanted to ask: what exactly is the difference between a tuple and a
    list? I'm sure there are some, but I can't seem to find a situation
    where I can use one but not the other.
    The differences are explained pretty good on here. Although, I am
    pretty new to Python as well.

    Learn how to create and manipulate tuples in Python. Discover the benefits of named tuples and how to sort a list of tuples. Start now!

    Comment

    • Alexander Schmolck

      #3
      Re: Tuple vs List: Whats the difference?

      Shafik <shafik23@gmail .comwrites:
      Hello folks,
      >
      I am an experienced programmer, but very new to python (2 days). I
      wanted to ask: what exactly is the difference between a tuple and a
      list? I'm sure there are some, but I can't seem to find a situation
      where I can use one but not the other.
      Try mutating the tuple or using the list as a hashkey.

      'as

      Comment

      • Shafik

        #4
        Re: Tuple vs List: Whats the difference?

        On Jul 11, 12:05 pm, Alexander Schmolck <a.schmo...@gma il.comwrote:
        Shafik <shafi...@gmail .comwrites:
        Hello folks,
        >
        I am an experienced programmer, but very new to python (2 days). I
        wanted to ask: what exactly is the difference between a tuple and a
        list? I'm sure there are some, but I can't seem to find a situation
        where I can use one but not the other.
        >
        Try mutating the tuple or using the list as a hashkey.
        >
        'as
        Cool thanks guys.

        --Shafik


        Comment

        • Dave Baum

          #5
          Re: Tuple vs List: Whats the difference?

          In article <1184177195.569 916.184770@22g2 000hsm.googlegr oups.com>,
          Shafik <shafik23@gmail .comwrote:
          Hello folks,
          >
          I am an experienced programmer, but very new to python (2 days). I
          wanted to ask: what exactly is the difference between a tuple and a
          list? I'm sure there are some, but I can't seem to find a situation
          where I can use one but not the other.
          Lists and tuples are both sequences, and can be used interchangeably in
          many situations. The big difference is that lists are mutable (can be
          modified), and tuples are immutable (cannot be modified). Lists also
          have a richer API (for example the index and count methods).

          If you are going to need to change the contents of the sequence
          (appending, removing, altering a value, etc), then use a list.

          If you require the sequence to be immutable (such as being used as the
          key in a dictionary), then use a tuple.

          In general, tuples should be a little more efficient than lists.
          However, altering a list is more efficient than creating a new tuple, so
          "always prefer tuples" does not necessarily lead to a faster overall
          program. Unless performance is critical, I wouldn't worry about this
          factor.

          IMHO, a more important reason to prefer tuples is that since they are
          immutable, you don't have to worry about side effects. If you call a
          function and pass it a list, you have no guarantee that the list won't
          be changed during the function call. With tuples, any attempt to change
          the tuple will raise an exception. If it is important to the caller
          that the sequence remain unchanged, then a tuple is a safer
          representation for that sequence.

          Dave

          Comment

          • Hendrik van Rooyen

            #6
            Re: Tuple vs List: Whats the difference?

            "Shafik" <s...3@gmail.co mwrote:

            Hello folks,
            >
            I am an experienced programmer, but very new to python (2 days). I
            wanted to ask: what exactly is the difference between a tuple and a
            list? I'm sure there are some, but I can't seem to find a situation
            where I can use one but not the other.
            Welcome to Python.
            >From a practical point of view, use lists - you will get exceptions
            if you use a list where a tuple is required, such as a key in a dict,
            and other more obscure cases.

            The philosophical differences - "tuple as struct" vs "list as collection"
            argument, makes no practical difference - you can use a list in the same
            structured way if you want, and lists have more nice methods as they
            are not immutable as tuples are.

            Google this Group for "Index" to see some flame wars.

            hth - Hendrik

            Comment

            • Ben Finney

              #7
              Re: Tuple vs List: Whats the difference?

              Dave Baum <Dave.Baum@moto rola.comwrites:
              In article <1184177195.569 916.184770@22g2 000hsm.googlegr oups.com>,
              Shafik <shafik23@gmail .comwrote:
              >
              what exactly is the difference between a tuple and a list? I'm
              sure there are some, but I can't seem to find a situation where I
              can use one but not the other.
              >
              Lists and tuples are both sequences, and can be used interchangeably
              in many situations. The big difference is that lists are mutable
              (can be modified), and tuples are immutable (cannot be modified).
              Lists also have a richer API (for example the index and count
              methods).
              This common misconception falls foul of only considering the
              implementation details.

              The choice of when to use one or the other is more one of intent: A
              tuple is best for heterogeneous sequences, a list is best for
              homogeneous sequences. Here's a better explanation:

              <URL:http://jtauber.com/blog/2006/04/15/python_tuples_a re_not_just_con stant_lists>

              --
              \ "No matter how cynical I get, I can't seem to keep up." -- |
              `\ Lily Tomlin |
              _o__) |
              Ben Finney

              Comment

              • Shafik

                #8
                Re: Tuple vs List: Whats the difference?

                Thanks to all of you, Im sure I'll pick your brains some more later
                down the line.

                --Shafik


                Comment

                Working...