tuple versus list

Discussion in 'Python' started by Xah Lee, Oct 16, 2005.

  1. Xah Lee

    Xah Lee Guest

    suppose i'm going to have a data structure like this:

    [
    [imgFullPath,(width, height)],
    [imgFullPath,(width, height)],
    [imgFullPath,(width, height)],
    [imgFullPath,(width, height)],
    ....
    ]

    should i use (width,height) or [width,height]?
    what advantage i get to use n-tuple instead of the generic list?

    Thanks.

    Xah

    ∑ http://xahlee.org/
    Xah Lee, Oct 16, 2005
    #1
    1. Advertising

  2. It's simple: if you want to modify the data structure after it has been
    created, use lists, otherwise tuples.

    Tuples are much more memory efficient, so your program will consume
    less memory and probably run faster. So preferably use tuples. However
    with tuples you can't do:
    t[0] = 'new value'
    t.append('new value')
    These statements are possible with lists.

    Stani
    --
    SPE - Stani's Python Editor (http://pythonide.stani.be)
    SPE - Stani's Python Editor, Oct 16, 2005
    #2
    1. Advertising

  3. Xah Lee

    Guest

    In this particular case, it seems that (width,height) looks nicer. But
    I think otherwise, list constuct is easier to read, even though it is
    supposed to be slower.

    With list you can :
    [a] + [ x for x in something ]

    With tuple it looks like this :
    (a,) + tuple(x for x in something)

    I think the list looks cleaner. And since you cannot concat tuple with
    list, I think unless it looks obvious and natural(as in your case), use
    list.

    Xah Lee wrote:
    > suppose i'm going to have a data structure like this:
    >
    > [
    > [imgFullPath,(width, height)],
    > [imgFullPath,(width, height)],
    > [imgFullPath,(width, height)],
    > [imgFullPath,(width, height)],
    > ...
    > ]
    >
    > should i use (width,height) or [width,height]?
    > what advantage i get to use n-tuple instead of the generic list?
    >
    > Thanks.
    >
    > Xah
    >
    > ∑ http://xahlee.org/
    , Oct 16, 2005
    #3
  4. Xah Lee

    Bryan Guest

    wrote:
    > In this particular case, it seems that (width,height) looks nicer. But
    > I think otherwise, list constuct is easier to read, even though it is
    > supposed to be slower.
    >
    > With list you can :
    > [a] + [ x for x in something ]
    >
    > With tuple it looks like this :
    > (a,) + tuple(x for x in something)
    >
    > I think the list looks cleaner. And since you cannot concat tuple with
    > list, I think unless it looks obvious and natural(as in your case), use
    > list.
    >



    i always use the structure analogy. if you view (width, height) as a structure,
    use a tuple. if you view it a sequence, use a list. in this example, i view it
    as a stucture, so i would use (width, height) as a tuple.

    bryan
    Bryan, Oct 16, 2005
    #4
  5. Xah Lee

    Donn Cave Guest

    In article <>,
    Bryan <> wrote:

    > wrote:
    > > In this particular case, it seems that (width,height) looks nicer. But
    > > I think otherwise, list constuct is easier to read, even though it is
    > > supposed to be slower.
    > >
    > > With list you can :
    > > [a] + [ x for x in something ]
    > >
    > > With tuple it looks like this :
    > > (a,) + tuple(x for x in something)
    > >
    > > I think the list looks cleaner. And since you cannot concat tuple with
    > > list, I think unless it looks obvious and natural(as in your case), use
    > > list.
    > >

    >
    >
    > i always use the structure analogy. if you view (width, height) as a
    > structure,
    > use a tuple. if you view it a sequence, use a list. in this example, i view
    > it
    > as a stucture, so i would use (width, height) as a tuple.


    Right, but there's an unfortunate ambiguity in the term "sequence",
    since in Python it is defined to include tuple. I gather you meant
    more in the abstract sense of a data collection whose interesting
    properties are of a sequential nature, as opposed to the way we are
    typically more interested in positional access to a tuple. Maybe
    a more computer literate reader will have a better word for this,
    that doesn't collide with Python terminology. My semi-formal
    operational definition is "a is similar to a[x:y], where
    x is not 0 or y is not -1, and `similar' means `could be a legal
    value in the same context.'"

    Donn Cave,
    Donn Cave, Oct 17, 2005
    #5
  6. Xah Lee

    Bryan Guest


    >>
    >>i always use the structure analogy. if you view (width, height) as a
    >>structure,
    >>use a tuple. if you view it a sequence, use a list. in this example, i view
    >>it
    >>as a stucture, so i would use (width, height) as a tuple.

    >
    >
    > Right, but there's an unfortunate ambiguity in the term "sequence",
    > since in Python it is defined to include tuple. I gather you meant
    > more in the abstract sense of a data collection whose interesting
    > properties are of a sequential nature, as opposed to the way we are
    > typically more interested in positional access to a tuple. Maybe
    > a more computer literate reader will have a better word for this,
    > that doesn't collide with Python terminology. My semi-formal
    > operational definition is "a is similar to a[x:y], where
    > x is not 0 or y is not -1, and `similar' means `could be a legal
    > value in the same context.'"
    >
    > Donn Cave,



    yes, you are correct. i shouldn't have used the word "sequence" which is a
    python term. maybe structure vs. array. in any case, i think the *wrong*
    answer that is often given to this question is along the lines of if it's read
    only, make it a tuple. if it's read write, make it a list. a great trivial
    example is a point. a point is a structure (x, y). if you have many points
    then you have a list of structures: [(x, y), (x1, y1), (x2, y2), ...]. to me,
    it doesn't matter if you want to modify a point. if you do then create a new
    one, but don't make it a list just to make it modifiable.

    bryan
    Bryan, Oct 18, 2005
    #6
    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. Michal Mikolajczyk
    Replies:
    1
    Views:
    803
    Larry Bates
    Apr 20, 2004
  2. Jeff Epler
    Replies:
    0
    Views:
    948
    Jeff Epler
    Apr 20, 2004
  3. Davy
    Replies:
    3
    Views:
    1,885
    Wildemar Wildenburger
    Nov 7, 2007
  4. Jeff Nyman
    Replies:
    8
    Views:
    382
    Terry Reedy
    Jun 5, 2008
  5. Paul Butcher
    Replies:
    12
    Views:
    702
    Gary Wright
    Nov 28, 2007
Loading...

Share This Page