Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > tuple versus list

Reply
Thread Tools

tuple versus list

 
 
Xah Lee
Guest
Posts: n/a
 
      10-16-2005
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://www.velocityreviews.com/forums/(E-Mail Removed)
http://xahlee.org/

 
Reply With Quote
 
 
 
 
SPE - Stani's Python Editor
Guest
Posts: n/a
 
      10-16-2005
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)

 
Reply With Quote
 
 
 
 
bonono@gmail.com
Guest
Posts: n/a
 
      10-16-2005
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
> (E-Mail Removed)
> ∑ http://xahlee.org/


 
Reply With Quote
 
Bryan
Guest
Posts: n/a
 
      10-16-2005
(E-Mail Removed) 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

 
Reply With Quote
 
Donn Cave
Guest
Posts: n/a
 
      10-17-2005
In article <(E-Mail Removed)>,
Bryan <(E-Mail Removed)> wrote:

> (E-Mail Removed) 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, (E-Mail Removed)
 
Reply With Quote
 
Bryan
Guest
Posts: n/a
 
      10-18-2005

>>
>>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, (E-Mail Removed)



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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating A Tuple From A List, Adding To Tuple As You Do Jeff Nyman Python 8 06-05-2008 09:04 PM
Re: Mozilla versus IE versus Opera versus Safari Peter Potamus the Purple Hippo Firefox 0 05-08-2008 12:56 PM
equal? versus eql? versus == versus === verus <=> Paul Butcher Ruby 12 11-28-2007 06:06 AM
List to Tuple and Tuple to List? Davy Python 3 11-07-2007 06:19 PM
Easily convert unicode tuple to python string tuple??? Michal Mikolajczyk Python 1 04-20-2004 08:37 PM



Advertisments