473,385 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

what is the difference between tuple and list?

is there any typical usage that shows their difference?

thanks

daniel

May 16 '06 #1
7 5782
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

May 16 '06 #2
daniel wrote:
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
May 16 '06 #3
> 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.

May 16 '06 #4
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
t = (1,2,3,4)
t[2] 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 -

May 16 '06 #5
On 16 May 2006 07:47:24 -0700, daniel <da*************@gmail.com> wrote:

<http://www.python.org/doc/faq/general.html#why-are-there-separate-tuple-and-list-data-types>

--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/
May 16 '06 #6
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.

May 16 '06 #7
infidel wrote:
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.


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

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
May 16 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Greg Smethells | last post by:
What exactly does "*values" mean in the following code? Is this a pointer to a PyList? Why can I not find good documentation on this any where? I must be blind: >>> from struct import * >>>...
23
by: Mike Meyer | last post by:
Ok, we've added list comprehensions to the language, and seen that they were good. We've added generator expressions to the language, and seen that they were good as well. I'm left a bit...
6
by: roopa | last post by:
I have declared a class with name List; and in main() function, i have declared the List object as follows class List { public: List() { cout<<"In List Constuctor";
2
by: diadia | last post by:
string s = "hello"; const char *p = s.begin(); cout << p << endl; // print hello s = ""; char *p2= s.begin(); cout << p2 << endl; // print hello why?????
11
by: J Wang | last post by:
dear, I debug the program recently as follows. #include <sys/stat.h> int main(int argc, char *argv) { struct stat buf;
3
by: Mirco Wahab | last post by:
Hi, I have a 2D array, maybe irregular, like arr = , , ] if tried to pull an index list
0
by: RupeshDeenadayalan | last post by:
Is there any difference between a boot loader and a boot strapper.It would be better if any one can tell what operations does a general bootloader,boot strapper,BSP,Driver does???
5
by: bhushanbagul | last post by:
Hi All Please let me know exactly what difference i and g makes in oracle version i.e. when we say oracle 10g or oracle 11i Thanks Bhushan
9
by: Stephan Steiner | last post by:
Hi I seem to have a bit of trouble understanding one bit of how generics work: In C#, every class automatically derives from object, and inherits a bunch of properties (i.e. ToString()). Thus,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.