473,657 Members | 2,513 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what is the difference between tuple and list?

is there any typical usage that shows their difference?

thanks

daniel

May 16 '06 #1
7 5796
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#wh y-are-there-separate-tuple-and-list-data-types>

--
Cheers,
Simon B,
si***@brunningo nline.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
2070
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 * >>> format = "dl" >>> values = >>> foo = pack(format, *values) >>> foo '\x1f\x85\xebQ\xb8\x1e\t@*\x00\x00\x00'
23
2253
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 confused, though - when would I use a list comp instead of a generator expression if I'm going to require 2.4 anyway? Thanks, <mike --
6
3225
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
3658
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
3405
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
2078
by: Mirco Wahab | last post by:
Hi, I have a 2D array, maybe irregular, like arr = , , ] if tried to pull an index list
0
2462
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
10428
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
6985
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, (MyClass is object) should always evaluate as true. However, if I have a method with the following signature:
0
8421
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8325
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8742
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8518
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8621
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7354
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.