Velocity Reviews > Lists and Tuples

# Lists and Tuples

Jeff Wagner
Guest
Posts: n/a

 12-05-2003
I've spent most of the day playing around with lists and tuples to get a really good grasp on what
you can do with them. I am still left with a question and that is, when should you choose a list or
a tuple? I understand that a tuple is immutable and a list is mutable but there has to be more to it
than just that. Everything I tried with a list worked the same with a tuple. So, what's the
difference and why choose one over the other?

Jeff

Paul Rubin
Guest
Posts: n/a

 12-05-2003
Jeff Wagner <(E-Mail Removed)> writes:
> I've spent most of the day playing around with lists and tuples to
> get a really good grasp on what you can do with them. I am still
> left with a question and that is, when should you choose a list or a
> tuple? I understand that a tuple is immutable and a list is mutable
> but there has to be more to it than just that. Everything I tried
> with a list worked the same with a tuple. So, what's the difference
> and why choose one over the other?

Try this with a list:

a = [1, 2, 3, 4, 5]
a[3] = 27
print a

Then try it with a tuple.

David Eppstein
Guest
Posts: n/a

 12-05-2003
In article <(E-Mail Removed)>,
Paul Rubin <http://(E-Mail Removed)> wrote:

> Try this with a list:
>
> a = [1, 2, 3, 4, 5]
> a[3] = 27
> print a
>
> Then try it with a tuple.

That's true, but another answer is: you should use tuples for short
sequences of diverse items (like the arguments to a function). You
should use lists for longer sequences of similar items.

--
David Eppstein http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science

Jeff Wagner
Guest
Posts: n/a

 12-05-2003
On 04 Dec 2003 21:31:12 -0800, Paul Rubin <http://(E-Mail Removed)> wrotf:

>Jeff Wagner <(E-Mail Removed)> writes:
>> I've spent most of the day playing around with lists and tuples to
>> get a really good grasp on what you can do with them. I am still
>> left with a question and that is, when should you choose a list or a
>> tuple? I understand that a tuple is immutable and a list is mutable
>> but there has to be more to it than just that. Everything I tried
>> with a list worked the same with a tuple. So, what's the difference
>> and why choose one over the other?

>
>
>Try this with a list:
>
> a = [1, 2, 3, 4, 5]
> a[3] = 27
> print a
>
>Then try it with a tuple.

That's because a tuple is immutable and a list is mutable but what else? I guess I said everything I
tried with a tuple worked with a list ... not mentioning I didn't try to break the immutable/mutable
rule I was aware of. Besides trying to change a tuple, I could cut it, slice and dice it just like I
could a list. They seemed to have the same built-in methods, too.

From what I can see, there is no reason for me to ever want to use a tuple and I think there is
something I am missing. Why would Guido go to all the effort to include tuples if (as it appears)
lists are just as good but more powerful ... you can change the contents of a list.

Jeff

Andrew Bennetts
Guest
Posts: n/a

 12-05-2003
On Fri, Dec 05, 2003 at 05:19:33AM +0000, Jeff Wagner wrote:
> I've spent most of the day playing around with lists and tuples to get a really good grasp on what
> you can do with them. I am still left with a question and that is, when should you choose a list or
> a tuple? I understand that a tuple is immutable and a list is mutable but there has to be more to it
> than just that. Everything I tried with a list worked the same with a tuple. So, what's the
> difference and why choose one over the other?

What's the difference?

>>> import sets
>>> sets.Set(dir(list)).difference(sets.Set(dir(tuple) ))

Set(['sort', 'index', '__delslice__', 'reverse', 'extend', 'insert',
'__setslice__', 'count', 'remove', '__setitem__', '__iadd__', 'pop',
'__delitem__', 'append', '__imul__'])

-Andrew.

Dennis Lee Bieber
Guest
Posts: n/a

 12-05-2003
Jeff Wagner fed this fish to the penguins on Thursday 04 December 2003
21:19 pm:

> tuple. So, what's the difference and why choose one over the other?
>

A tuple can be the key for a dictionary, a list can't.

Not sure of a tuple containing a list, though...

--
> ================================================== ============ <
> http://www.velocityreviews.com/forums/(E-Mail Removed) | Wulfraed Dennis Lee Bieber KD6MOG <
> (E-Mail Removed) | Bestiaria Support Staff <
> ================================================== ============ <

Douglas Alan
Guest
Posts: n/a

 12-05-2003
David Eppstein <(E-Mail Removed)> writes:

> That's true, but another answer is: you should use tuples for short
> sequences of diverse items (like the arguments to a function). You
> should use lists for longer sequences of similar items.

I disagree. You should use a tuple when you wish to not change the
contents once you have constructed the sequence, and otherwise you
should use a list. Using a tuple can make your code clearer by
letting the reader know from the beginning that the contents won't be
changing.

Fredrik Lundh actually called me names a couple years back for
asserting this, but Python luminary (and rude fellow) or not, he is

You don't have to take my word for it, though, since Python itself
uses tuples in this manner, in the form of the container used for
excess arguments (excess arguments certainly don't have to be short,
and they are generally homogeneous, not heterogeneous), and Python
Mega Widgets (for example) is littered with code that looks like:

optiondefs = (
('initwait', 500, None), # milliseconds
('label_background', 'lightyellow', None),
('label_foreground', 'black', None),
('label_justify', 'left', None),
('master', 'parent', None),
('relmouse', 'none', self._relmouse),
('state', 'both', self._state),
('statuscommand', None, None),
('xoffset', 20, None), # pixels
('yoffset', 1, None), # pixels
('hull_highlightthickness', 1, None),
('hull_highlightbackground', 'black', None),
)

In the above case we see tuples being used both as records *and* as an
arbitrary-length sequence of homogenious elements. Why is a tuple
being used in the latter case, rather than a list? Because the
sequence isn't going to be modified.

|>oug

Bengt Richter
Guest
Posts: n/a

 12-05-2003
On Thu, 04 Dec 2003 21:45:23 -0800, David Eppstein <(E-Mail Removed)> wrote:

>In article <(E-Mail Removed)>,
> Paul Rubin <http://(E-Mail Removed)> wrote:
>
>> Try this with a list:
>>
>> a = [1, 2, 3, 4, 5]
>> a[3] = 27
>> print a
>>
>> Then try it with a tuple.

>
>That's true, but another answer is: you should use tuples for short
>sequences of diverse items (like the arguments to a function). You
>should use lists for longer sequences of similar items.
>

I'm curious what you're getting at. I.e., what does diversity or
similarity have to do with the choice? Is that an aesthetic thing?
(In which case 'should' should be qualified a bit, IWT
Or what am I missing?

Regards,
Bengt Richter

Douglas Alan
Guest
Posts: n/a

 12-05-2003
X-Draft-From: ("comp.lang.python" 285349)
To: (E-Mail Removed) (Bengt Richter)
Subject: Re: Lists and Tuples
References: <(E-Mail Removed)>
<(E-Mail Removed)>
<(E-Mail Removed)>
<bqpfde\$426\$0@216.39.172.122>
Fcc: |rcvstore +articles
From: Douglas Alan <(E-Mail Removed)>
--text follows this line--
(E-Mail Removed) (Bengt Richter) writes:

> On Thu, 04 Dec 2003 21:45:23 -0800, David Eppstein
> <(E-Mail Removed)> wrote:

>> That's true, but another answer is: you should use tuples for short
>> sequences of diverse items (like the arguments to a function). You
>> should use lists for longer sequences of similar items.

> I'm curious what you're getting at. I.e., what does diversity or
> similarity have to do with the choice?

Nothing really, except by idiom. When people use "should" here, I
think they are over-generalizing. Most of the time, records (short
and heterogenious) are used in a read-only fashion, and long
homogenous sequences are used in a read-write fashion. But when
people characterize this tendency with a "should", I think they are
making a thinko. There are times when you need to modify a record and
consequently might use a dictionary or list, rather than a tuple,
and there are also times when you will never want to modify a long,
homogenous sequence, in which case many people would find it more
elegant to use a tuple than to use a list.

The reason for the "should" is probably because, I imagine, Guido had
in mind mostly multiple return values from function, and the like,
when he put tuples into the language.

|>oug

Joe Francia
Guest
Posts: n/a

 12-05-2003
Jeff Wagner wrote:
> I've spent most of the day playing around with lists and tuples to get a really good grasp on what
> you can do with them. I am still left with a question and that is, when should you choose a list or
> a tuple? I understand that a tuple is immutable and a list is mutable but there has to be more to it
> than just that. Everything I tried with a list worked the same with a tuple. So, what's the
> difference and why choose one over the other?
>

According to the Python FAQ:

------------------------------------------------------------------------
4.15 Why are there separate tuple and list data types?

Lists and tuples, while similar in many respects, are generally used in
fundamentally different ways. Tuples can be thought of as being similar
to Pascal records or C structs; they're small collections of related
data which may be of different types which are operated on as a group.
For example, a Cartesian coordinate is appropriately represented as a
tuple of two or three numbers.

Lists, on the other hand, are more like arrays in other languages. They
tend to hold a varying number of objects all of which have the same type
and which are operated on one-by-one. For example, os.listdir('.')
returns a list of strings representing the files in the current
directory. Functions which operate on this output would generally not
break if you added another file or two to the directory.

Tuples are immutable, meaning that once a tuple has been created, you
can't replace any of its elements with a new value. Lists are mutable,
meaning that you can always change a list's elements. Only immutable
elements can be used as dictionary keys, and hence only tuples and not
lists can be used as keys.

http://www.python.org/doc/faq/genera...ist-data-types
------------------------------------------------------------------------

Of course, this information will prevent neither flame wars, nor you
from using them how you wish (within the boundaries of the language).
However you choose to use them, just be clear and consistent.

Peace,
Joe