473,659 Members | 2,667 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

tuple versus list

suppose i'm going to have a data structure like this:

[
[imgFullPath,(wi dth, height)],
[imgFullPath,(wi dth, height)],
[imgFullPath,(wi dth, height)],
[imgFullPath,(wi dth, height)],
....
]

should i use (width,height) or [width,height]?
what advantage i get to use n-tuple instead of the generic list?

Thanks.

Xah
xa*@xahlee.org
∑ http://xahlee.org/

Oct 16 '05 #1
5 3616
It's simple: if you want to modify the data structure after it has been
created, use lists, otherwise tuples.

Tuples are much more memory efficient, so your program will consume
less memory and probably run faster. So preferably use tuples. However
with tuples you can't do:
t[0] = 'new value'
t.append('new value')
These statements are possible with lists.

Stani
--
SPE - Stani's Python Editor (http://pythonide.stani.be)

Oct 16 '05 #2
In this particular case, it seems that (width,height) looks nicer. But
I think otherwise, list constuct is easier to read, even though it is
supposed to be slower.

With list you can :
[a] + [ x for x in something ]

With tuple it looks like this :
(a,) + tuple(x for x in something)

I think the list looks cleaner. And since you cannot concat tuple with
list, I think unless it looks obvious and natural(as in your case), use
list.

Xah Lee wrote:
suppose i'm going to have a data structure like this:

[
[imgFullPath,(wi dth, height)],
[imgFullPath,(wi dth, height)],
[imgFullPath,(wi dth, height)],
[imgFullPath,(wi dth, height)],
...
]

should i use (width,height) or [width,height]?
what advantage i get to use n-tuple instead of the generic list?

Thanks.

Xah
xa*@xahlee.org
∑ http://xahlee.org/


Oct 16 '05 #3
bo****@gmail.co m wrote:
In this particular case, it seems that (width,height) looks nicer. But
I think otherwise, list constuct is easier to read, even though it is
supposed to be slower.

With list you can :
[a] + [ x for x in something ]

With tuple it looks like this :
(a,) + tuple(x for x in something)

I think the list looks cleaner. And since you cannot concat tuple with
list, I think unless it looks obvious and natural(as in your case), use
list.

i always use the structure analogy. if you view (width, height) as a structure,
use a tuple. if you view it a sequence, use a list. in this example, i view it
as a stucture, so i would use (width, height) as a tuple.

bryan

Oct 16 '05 #4
In article <ma************ *************** **********@pyth on.org>,
Bryan <be****@gmail.c om> wrote:
bo****@gmail.co m wrote:
In this particular case, it seems that (width,height) looks nicer. But
I think otherwise, list constuct is easier to read, even though it is
supposed to be slower.

With list you can :
[a] + [ x for x in something ]

With tuple it looks like this :
(a,) + tuple(x for x in something)

I think the list looks cleaner. And since you cannot concat tuple with
list, I think unless it looks obvious and natural(as in your case), use
list.

i always use the structure analogy. if you view (width, height) as a
structure,
use a tuple. if you view it a sequence, use a list. in this example, i view
it
as a stucture, so i would use (width, height) as a tuple.


Right, but there's an unfortunate ambiguity in the term "sequence",
since in Python it is defined to include tuple. I gather you meant
more in the abstract sense of a data collection whose interesting
properties are of a sequential nature, as opposed to the way we are
typically more interested in positional access to a tuple. Maybe
a more computer literate reader will have a better word for this,
that doesn't collide with Python terminology. My semi-formal
operational definition is "a is similar to a[x:y], where
x is not 0 or y is not -1, and `similar' means `could be a legal
value in the same context.'"

Donn Cave, do**@u.washingt on.edu
Oct 17 '05 #5

i always use the structure analogy. if you view (width, height) as a
structure,
use a tuple. if you view it a sequence, use a list. in this example, i view
it
as a stucture, so i would use (width, height) as a tuple.

Right, but there's an unfortunate ambiguity in the term "sequence",
since in Python it is defined to include tuple. I gather you meant
more in the abstract sense of a data collection whose interesting
properties are of a sequential nature, as opposed to the way we are
typically more interested in positional access to a tuple. Maybe
a more computer literate reader will have a better word for this,
that doesn't collide with Python terminology. My semi-formal
operational definition is "a is similar to a[x:y], where
x is not 0 or y is not -1, and `similar' means `could be a legal
value in the same context.'"

Donn Cave, do**@u.washingt on.edu

yes, you are correct. i shouldn't have used the word "sequence" which is a
python term. maybe structure vs. array. in any case, i think the *wrong*
answer that is often given to this question is along the lines of if it's read
only, make it a tuple. if it's read write, make it a list. a great trivial
example is a point. a point is a structure (x, y). if you have many points
then you have a list of structures: [(x, y), (x1, y1), (x2, y2), ...]. to me,
it doesn't matter if you want to modify a point. if you do then create a new
one, but don't make it a list just to make it modifiable.

bryan

Oct 18 '05 #6

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

Similar topics

10
5205
by: Carlo v. Dango | last post by:
Hello there. I have a function which as an argument takes a tuple and either returns that tuple or a mutated version of it. The problem is that tuples are imutable, hence I have to create a new tuple and copy the content of the old tuple to a new one. But how do I do this if I only at runtime know the size of the tuple? I wish I could pass around lists instead.. that would be so much easier, but I'm passing "*args" and "**kwargs" around...
9
4072
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 already created :/ how to do it? thx.
2
2084
by: Ishwar Rattan | last post by:
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
37
2373
by: Gregor Horvath | last post by:
Hi, >>>type() <type 'list'> >>>type(('1')) <type 'str'> I wonder why ('1') is no tuple????
9
19197
by: Odd-R. | last post by:
I have a dictionary, and I want to convert it to a tuple, that is, I want each key - value pair in the dictionary to be a tuple in a tuple. If this is the dictionary {1:'one',2:'two',3:'three'}, then I want this to be the resulting tuple: ((1,'one'),(2,'two'),(3,'three')). I have been trying for quite some time now, but I do not get the result as I want it. Can this be done, or is it not
77
4411
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 Maclaren.
6
2000
by: fdu.xiaojf | last post by:
Hi all, I can use list comprehension to create list quickly. So I expected that I can created tuple quickly with the same syntax. But I found that the same syntax will get a generator, not a tuple. Here is my example: In : a = (i for i in range(10)) In : b =
8
16282
by: royG | last post by:
hi i am trying to resize some images.First i'd read the size as a 2 tuple and then i want to divide it by 2 or 4 or 2.5 etc.. suppose origsz=(400,300) i want to divide the origsize by 2.5 so i can resize to (160,120) scale=2.5 how can i get the newsz?
0
1787
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 reasonable since generators require iteration and stop iteration and what not.
0
8339
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
8751
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...
0
8629
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
7360
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
6181
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
5650
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();...
0
4176
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
4338
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1982
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.