473,385 Members | 1,379 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.

lists v. tuples

What are the considerations in choosing between:

return [a, b, c]

and

return (a, b, c) # or return a, b, c

Why is the immutable form the default?
Mar 17 '08 #1
8 1271
On Mar 17, 6:49*am, MartinRineh...@gmail.com wrote:
What are the considerations in choosing between:

* *return [a, b, c]

and

* * return (a, b, c) # or return a, b, c

Why is the immutable form the default?
Using a house definition from some weeks ago, a tuple is a data
structure such which cannot contain a refrence to itself. Can a
single expression refer to itself ever?
Mar 17 '08 #2
On Mar 17, 12:28 pm, castiro...@gmail.com wrote:
Why is the immutable form the default?

Using a house definition from some weeks ago, a tuple is a data
structure such which cannot contain a refrence to itself. Can a
single expression refer to itself ever?
Can't imagine why that feature was highlighted in particular, but a
list can reference itself even though an expression can't.

The following example looks a bit self-referential, but isn't...

a = 2
a = [1, a, 3] # result [1, 2, 3]

The following additional line, however does create a self-referencing
list...

a [1] = a

The result being [1, [...], 2]

It's nice to see that Python can handle the output for this without
going into an infinite recursion - which is exactly what it used to do
in the distant past.

A tuple cannot be made to reference itself because it cannot be
modified after creation. The key point is that lists are mutable,
whereas tuples are not.
Mar 17 '08 #3
On Mon, 17 Mar 2008 05:28:19 -0700, castironpi wrote:
a tuple is a data
structure such which cannot contain a refrence to itself.

>>a = [] # a list
b = (a, None) # a tuple
a.append(b)
print b
([([...], None)], None)
>>b[0][0] is b
True
So, yes tuples can contain a reference to themselves, but only indirectly.
Can a single expression refer to itself ever?
You can't refer to an object until it exists, so no.


--
Steven
Mar 17 '08 #4
Ma************@gmail.com wrote:
What are the considerations in choosing between:

return [a, b, c]

and

return (a, b, c) # or return a, b, c
A common explanation for this is that lists are for homogenous
collections, tuples are for when you have heterogenous collections i.e.
related but different things.

If you follow this line of argument then when you want to return some
values from a function, e.g. url, headers and data a tuple would be the
appropriate thing to use.

If you really are returning a homogenous collection (e.g. top 5 urls)
then a list would be more appropriate.

Another way to look at it is what you expect the caller to do with the
results. If they are just going to unpack a fixed set of values then a
tuple makes sense. If you write:

return url, headers, data

then the caller writing:

url, headers, data = frobozz()

has a certain symmetry.

It isn't set in stone of course: use whatever you want or whatever feels
right at the time.
Why is the immutable form the default?
It isn't. It uses whichever type you specify but tuples may involve a
little less typing.
Mar 17 '08 #5
Ninereeds <st*************@aol.comwrote:
On Mar 17, 1:31 pm, Duncan Booth <duncan.bo...@invalid.invalidwrote:
>A common explanation for this is that lists are for homogenous
collections, tuples are for when you have heterogenous collections i.e.
related but different things.

I interpret this as meaning that in a data table, I should have a list
of records but each record should be a tuple of fields, since the
fields for a table usually have different forms whereas the records
usually all have the same record layout.
That is indeed what Python's Database API usually does (although it doesn't
mandate it):

.fetchmany([size=cursor.arraysize])

Fetch the next set of rows of a query result, returning a
sequence of sequences (e.g. a list of tuples). An empty
sequence is returned when no more rows are available.
Mar 17 '08 #6
On Mar 17, 1:31 pm, Duncan Booth <duncan.bo...@invalid.invalidwrote:
>
>A common explanation for this is that lists are for homogenous
>collections, tuples are for when you have heterogenous collections i.e.
>related but different things.
I interpret this as meaning that in a data table, I should have a list
of records but each record should be a tuple of fields, since the
fields for a table usually have different forms whereas the records
usually all have the same record layout.
>b in b
False
That's actually interesting.
>>a= []
a.append( a )
a
[[...]]
>>a in a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: maximum recursion depth exceeded in cmp
Mar 18 '08 #7
ca********@gmail.com wrote:
On Mar 17, 1:31 pm, Duncan Booth <duncan.bo...@invalid.invalid>
wrote:
>A common explanation for this is that lists are for homogenous
collections, tuples are for when you have heterogenous
collections i.e. related but different things.
I interpret this as meaning that in a data table, I should have a
list of records but each record should be a tuple of fields,
since the fields for a table usually have different forms whereas
the records usually all have the same record layout.
>>b in b
False

That's actually interesting.
Just for the avoidance of doubt, I didn't write the 'b in b' line:
castironpi is replying to himself without attribution.

P.S. I still don't see the relevance of any of castironpi's followup to my
post, but since none it made any sense to me I guess it doesn't matter.
Mar 18 '08 #8
On Mar 18, 5:34 pm, Duncan Booth <duncan.bo...@invalid.invalidwrote:
castiro...@gmail.com wrote:
On Mar 17, 1:31 pm, Duncan Booth <duncan.bo...@invalid.invalid>
wrote:
>A common explanation for this is that lists are for homogenous
>collections, tuples are for when you have heterogenous
>collections i.e. related but different things.
I interpret this as meaning that in a data table, I should have a
list of records but each record should be a tuple of fields,
since the fields for a table usually have different forms whereas
the records usually all have the same record layout.
>b in b
False
That's actually interesting.

Just for the avoidance of doubt, I didn't write the 'b in b' line:
castironpi is replying to himself without attribution.

P.S. I still don't see the relevance of any of castironpi's followup to my
post, but since none it made any sense to me I guess it doesn't matter.

Plus, it does work fine over here:

Python 2.5.1 (r251:54863, May 8 2007, 14:46:30)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>a = []
a.append(a)
a
[[...]]
>>a in a
True
George
Mar 19 '08 #9

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

Similar topics

10
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
42
by: Jeff Wagner | last post by:
I've spent most of the day playing around with lists and tuples to get a really good grasp on what you can do with them. I am still left with a question and that is, when should you choose a list or...
2
by: beliavsky | last post by:
Tuples, unlike lists, are immutable, which I crudely translate to mean "their contents cannot be changed". Out of habit, I use only lists, not tuples, in my code. But I wonder if this is poor style...
1
by: Gabriel B. | last post by:
I just sent an email asking for hints on how to import data into a python program As i said earlier i'm really new to python and besides being confortable with the syntax, i'm not sure if i'm on...
10
by: Philippe C. Martin | last post by:
Hi, I'm looking for an easy algorithm - maybe Python can help: I start with X lists which intial sort is based on list #1. I want to reverse sort list #1 and have all other lists sorted...
7
by: MTD | last post by:
Hello, I'm wondering if there's a quick way of resolving this problem. In a program, I have a list of tuples of form (str,int), where int is a count of how often str occurs e.g. L = would...
1
by: Simon Forman | last post by:
I've got a function that I'd like to improve. It takes a list of lists and a "target" element, and it returns the set of the items in the lists that appear either before or after the target...
12
by: rshepard | last post by:
I'm a bit embarrassed to have to ask for help on this, but I'm not finding the solution in the docs I have here. Data are assembled for writing to a database table. A representative tuple looks...
2
by: KDawg44 | last post by:
Hi, I am trying to learn python. I am working through a tutorial on python.org. I am trying to figure out how lists are different than tuples other than changing values at specific indices. ...
27
by: seberino | last post by:
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.