what is the difference between tuple and list?

D

danmcleran

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
 
D

Diez B. Roggisch

daniel said:
is there any typical usage that shows their difference?
d = {}
d[('multi', 'part', 'key')] = 'schnarz'
d[['multi', 'part', 'key']] = 'schnarz'
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
 
I

infidel

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.
 
H

Harold Fellermann

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
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:
t = ({},) # tuple with empty dict as its only item
t[0]["foo"] = "bar"

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 -
 
D

daniel

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.
 
B

bruno at modulix

infidel said:
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.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,297
Messages
2,571,525
Members
48,249
Latest member
reactnativeexpert

Latest Threads

Top