473,395 Members | 2,783 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,395 software developers and data experts.

List and tuple usage distinction??

I am a little confused about a list and a tuple.

Both can have dissimilar data-type elements, can be returned
by functions. The only difference that I see is that list is
mutable and tuple is not (of course list have .append() etc.)

What is a possible scenario where one is preferred over the other?

-ishwar
Jul 18 '05 #1
2 2064
Ishwar Rattan wrote:
I am a little confused about a list and a tuple.

Both can have dissimilar data-type elements, can be returned
by functions. The only difference that I see is that list is
mutable and tuple is not (of course list have .append() etc.)

What is a possible scenario where one is preferred over the other?

-ishwar

Lots of reasons ;-)

1. Maybe you want to create a dict and key off of a list of items. I
haven't had to do that, but it's feasible. Take a peek at this:
f = ('a', 'b', 'c')
g = list(f)
f ('a', 'b', 'c') g ['a', 'b', 'c'] h = {}
h[f] = 1
h[g] = 1 Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: list objects are unhashable h {('a', 'b', 'c'): 1}

You can key off of a tuple (the tuple "f") and you can't off of a plain
old list ("g").

2. You don't need the list of items to change or similarly
3. You want to make it more difficult for a list of items to change.
You can still fairly easily change a tuple - sort of: f = list(f)
f.append('d')
f = tuple(f)
f ('a', 'b', 'c', 'd')

What you've actually done is change the tuple object that "f" is
pointing to.

Someone else can answer better about memory usage, but I would think
that a tuple would cost less memory than a comparably sized list, so
that also may be a consideration.

Jeremy

Jul 18 '05 #2
Ishwar Rattan wrote:
I am a little confused about a list and a tuple.

Both can have dissimilar data-type elements, can be returned
by functions. The only difference that I see is that list is
mutable and tuple is not (of course list have .append() etc.)

What is a possible scenario where one is preferred over the other?


Does this answer in the FAQ list help?

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

-Peter
Jul 18 '05 #3

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

Similar topics

17
by: Gordon Airport | last post by:
Has anyone suggested introducing a mutable string type (yes, of course) and distinguishing them from standard strings by the quote type - single or double? As far as I know ' and " are currently...
23
by: Fuzzyman | last post by:
Pythons internal 'pointers' system is certainly causing me a few headaches..... When I want to copy the contents of a variable I find it impossible to know whether I've copied the contents *or*...
9
by: Yomanium Yoth Taripoät II | last post by:
HI, 1) what are the differences between list and tuple? 2) how to concatenate tuple and list? no method, no opérator? 3) im looking the fucking manual, and cant add value in my tuple, when it...
4
by: GrelEns | last post by:
hello, i wonder if this possible to subclass a list or a tuple and add more attributes ? also does someone have a link to how well define is own iterable object ? what i was expecting was...
3
by: Dan | last post by:
First, I'm sorry if this question has been asked too many times. I'm new to this news group. The question has to do with the use of popup windows in a web page. I have heard that popup windows...
5
by: Xah Lee | last post by:
suppose i'm going to have a data structure like this: , , , , .... ] should i use (width,height) or ?
7
by: daniel | last post by:
is there any typical usage that shows their difference? thanks daniel
77
by: Nick Maclaren | last post by:
Why doesn't the tuple type have an index method? It seems such a bizarre restriction that there must be some reason for it. Yes, I know it's a fairly rare requirement. Regards, Nick...
0
by: Hatem Nassrat | last post by:
on Wed Jun 13 10:17:24 CEST 2007, Diez B. Roggisch deets at nospam.web.de wrote: Well I have looked into this and it seems that using the list comprehension is faster, which is...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
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,...

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.