473,609 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

When are immutable tuples *essential*? Why can't you just use lists *everywhere* instead?

Please help me think of an example where immutable tuples are
essential.

It seems that everywhere a tuple is used one could just as easily use
a list instead.

chris

Apr 20 '07 #1
27 1665
se******@spawar .navy.mil wrote:
Please help me think of an example where immutable tuples are
essential.
When used as dictionary keys (also everywhere else where they must
be in a constant order).

Yes, this *is* used in practice.

Regards,
Björn

--
BOFH excuse #14:

sounds like a Windows problem, try calling Microsoft support

Apr 20 '07 #2
On Apr 20, 2:06 pm, "seber...@spawa r.navy.mil"
<seber...@spawa r.navy.milwrote :
Please help me think of an example where immutable tuples are
essential.

It seems that everywhere a tuple is used one could just as easily use
a list instead.

chris

I don't remember exactly where I read about it, but Guido said once
that tuples are being kept mainly for historical reasons.
This is what Python uses internally from the beginning and it's just
an implementation detail that makes sense in some contexts.
Although I guess you can ignore them and use only lists instead if you
want..

luis

Apr 20 '07 #3
Luis M. González wrote:
I don't remember exactly where I read about it, but Guido said
once that tuples are being kept mainly for historical reasons.
Weren't tuples added when lists already existed?

Regards,
Björn

--
BOFH excuse #101:

Collapsed Backbone

Apr 20 '07 #4
Iou need only consider having cartesian coordinate sets as the keys
for an example. A 2 dimensional list might be overly expensive if your
coordinates span a large area but are relatively sparse.

Apr 20 '07 #5
En Fri, 20 Apr 2007 15:28:51 -0300, Bjoern Schliessmann
<us************ **************@ spamgourmet.com escribió:
Luis M. González wrote:
>I don't remember exactly where I read about it, but Guido said
once that tuples are being kept mainly for historical reasons.

Weren't tuples added when lists already existed?
Both listobject.c and tupleobject.c appear to had been checked in at the
same time:

Revision 2167 - (view) (download) (as text) - [select for diffs]
Added Sun Oct 14 12:07:46 1990 UTC (16 years, 6 months ago) by guido
File length: 4965 byte(s)
Initial revision

And that's before the earliest tagged release I could find on svn, Python
0.98

--
Gabriel Genellina
Apr 20 '07 #6
On Apr 20, 3:28 pm, Bjoern Schliessmann <usenet-
mail-0306.20.chr0n.. .@spamgourmet.c omwrote:
Luis M. González wrote:
I don't remember exactly where I read about it, but Guido said
once that tuples are being kept mainly for historical reasons.

Weren't tuples added when lists already existed?

Regards,

Björn

--
BOFH excuse #101:

Collapsed Backbone
I tried googling for these comments, but I couldn't find them.
Perhaps I never read them and it was just my imagination...
Anyway, I suggest reading this chapter of "Dive into Python" for a
good explanation of the differences between tuples and lists:
http://diveintopython.org/native_data_types/tuples.html

The article explains that, amongst other things, tuples are faster
than lists, so if you are working with constant values (inmutables)
they are more indicated than lists.

luis
Apr 20 '07 #7
On Apr 20, 6:09 pm, Bjoern Schliessmann <usenet-
mail-0306.20.chr0n.. .@spamgourmet.c omwrote:
seber...@spawar .navy.mil wrote:
Please help me think of an example where immutable tuples are
essential.

When used as dictionary keys (also everywhere else where they must
be in a constant order).

Yes, this *is* used in practice.
Yup - using tuples as dictionary keys is very common.

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml
Regards,

Björn

--
BOFH excuse #14:

sounds like a Windows problem, try calling Microsoft support

Apr 20 '07 #8

The article explains that, amongst other things, tuples are faster
than lists, so if you are working with constant values (inmutables)
they are more indicated than lists.
Thanks. I thought Python's design wasn't so concerned with
optimizations.
Adding a new type "just" for optimization reasons seems perhaps
unnecessary. I could be wrong.

Apr 20 '07 #9
On Apr 21, 7:09 am, Luis M. González <luis...@gmail. comwrote:
On Apr 20, 3:28 pm, Bjoern Schliessmann <usenet-

mail-0306.20.chr0n.. .@spamgourmet.c omwrote:
Luis M. González wrote:
I don't remember exactly where I read about it, but Guido said
once that tuples are being kept mainly for historical reasons.
Weren't tuples added when lists already existed?
Regards,
Björn
--
BOFH excuse #101:
Collapsed Backbone

I tried googling for these comments, but I couldn't find them.
Perhaps I never read them and it was just my imagination...
Anyway, I suggest reading this chapter of "Dive into Python" for a
good explanation of the differences between tuples and lists:http://diveintopython.org/native_data_types/tuples.html

The article explains that, amongst other things, tuples are faster
than lists, so if you are working with constant values (inmutables)
they are more indicated than lists.
One inessential but very useful thing about tuples when you have a lot
of them is that they are allocated the minimum possible amount of
memory. OTOH lists are created with some slack so that appending etc
can avoid taking quadratic time.

Apr 20 '07 #10

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

Similar topics

10
7037
by: Ivan Voras | last post by:
Are there any performance/size differences between using tuples and using lists? -- -- Every sufficiently advanced magic is indistinguishable from technology - Arthur C Anticlarke
1
1759
by: Anne Wangnick | last post by:
Dear all, I don't get why the index() method is only defined for mutable sequence types. This is not what I expected. Shouldn't this be added in Python? Is there such a PEP already? Regards, Sebastian Wangnick
5
1679
by: Chris | last post by:
Hi Consider the following tuples: >>> t = (,) >>> u = (1,2) >>> v = ('1','2') >>> t (, ) >>> u (1, 2)
7
2678
by: Torsten Mohr | last post by:
Hi, reading the documentation (and also from a hint from this NG) i know now that there are some types that are not mutable. But why is it this way? From an overhead point of view i think it is not optimal, for example for a large string it could be much faster to have it changed in place, not generating a new one for
14
12270
by: Richard | last post by:
I have a large list of two element tuples. I want two separate lists: One list with the first element of every tuple, and the second list with the second element of every tuple. Each tuple contains a datetime object followed by an integer. Here is a small sample of the original list: ((datetime.datetime(2005, 7, 13, 16, 0, 54), 315), (datetime.datetime(2005, 7, 13, 16, 6, 12), 313),
90
4368
by: Ben Finney | last post by:
Howdy all, How can a (user-defined) class ensure that its instances are immutable, like an int or a tuple, without inheriting from those types? What caveats should be observed in making immutable instances? -- \ "Love is the triumph of imagination over intelligence." -- |
66
3481
by: Mike Meyer | last post by:
It seems that the distinction between tuples and lists has slowly been fading away. What we call "tuple unpacking" works fine with lists on either side of the assignment, and iterators on the values side. IIRC, "apply" used to require that the second argument be a tuple; it now accepts sequences, and has been depreciated in favor of *args, which accepts not only sequences but iterators. Is there any place in the language that still...
12
1631
by: John Henry | last post by:
Hi list, Just to make sure I understand this. Since there is no "pointer" type in Python, I like to know how I do that. For instance, if I do: ...some_huge_list is a huge list...
122
5474
by: C.L. | last post by:
I was looking for a function or method that would return the index to the first matching element in a list. Coming from a C++ STL background, I thought it might be called "find". My first stop was the Sequence Types page of the Library Reference (http://docs.python.org/lib/typesseq.html); it wasn't there. A search of the Library Reference's index seemed to confirm that the function did not exist. A little later I realized it might be called...
0
8091
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
8579
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8555
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
8232
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
8408
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...
1
6064
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
5524
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1686
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.