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

Home Posts Topics Members FAQ

Lists and Tuples and Much More

I'm going to start grouping all my questions in one post as this is my
second today, and sorta makes me feel dumb to keep having to bother you all
with trivial questions. I'll just seperate my questions with:
-------------------------------------------------------------------------------------------
Now onto the issue:
List's and Tuple's
I don't see the distinction between the two. I mean, I understand that a
list is mutable and a tuple is immutable.
The thing that I dont understand about them is what, besides that, seperates
the two. I did a little experimentation to try to understand it better, but
only confused myelf more.

A list looks like this:
>>>my_list = [1, 2, 3, 4, 5, 6]
and a tuple looks like this:
>>>my_tuple = (1, 2, 3, 4, 5, 6)
Now you can add to a list, but not a tuple so:
>>>my_list.appe nd(my_tuple) #or extend for that matter right?
[1, 2, 3, 4, 5, 6, (1, 2, 3, 4, 5, 6)]

Is that pretty much accurate? And which is better on resources....I' m
guessing a tuple seeing as it doesn't change.

And the last example brings up another question. What's the deal with a
tupple that has a list in it such as:
>>>my_tupple = (1, 2, 3, 4, 5, [6, 7, 8, 9])
Now I read somewhere that you could change the list inside that tupple. But
I can't find any documentation that describes HOW to do it. The only things
I CAN find on the subject say, "Don't do it because its more trouble than
it's worth." But that doesn't matter to me, because I want to know
everything.
---------------------------------------------------------------------------------------------

Now there comes append. I read everywhere that append only add's 1 element
to the end of your list. But if you write:
>>my_list = [1, 2, 3, 4, 5, 6]
my_list.appen d([7, 8, 9, 10])
my_list
[1, 2, 3, 4, 5, 6, [7, 8, 9, 10]]

Is that because list's, no matter what they contain, are counted as 1
element?

And how would you sort the list that's in the list? I guess that goes in
conjunction with the section above, but still:
>>my_list = [6, 4, 3, 5, 2, 1]
my_list.appen d([7, 9, 8, 10])
my_list.sort( )
my_list
[1, 2, 3, 4, 5, 6, [7, 9, 8, 10]]

This is, again, something I'm finding nothing on.
---------------------------------------------------------------------------------------------

Maybe I'm just not looking in the right spots. The only things I have as
learning aids are: this newsgroup ;p, http://diveintopython.org,
http://python.org/, Beggining Python: From Novice to Professional, and (now
don't laugh) Python for Dummies.
Apr 12 '07 #1
15 1502
Scott:

Others will give you many more answers, here is just a bit.
And how would you sort the list that's in the list? I guess that goes in
conjunction with the section above, but still:
>>my_list = [6, 4, 3, 5, 2, 1]
my_list.append ([7, 9, 8, 10])
my_list.sort ()
my_list

[1, 2, 3, 4, 5, 6, [7, 9, 8, 10]]
Such sorting may be impossible in Python 3.0 (comparing the order of
lists with integers may be seen as meaningless. Otherwise you can see
single numbers as lists of len=1, like another language does).

This is, again, something I'm finding nothing on.
Maybe because documentation requires some generalization capabilities.
A list is a single object, and it can contain a sequence of objects.

Bye,
bearophile

Apr 12 '07 #2
Now I read somewhere that you could change the
list inside that tupple. But I can't find any
documentation that describes HOW to do it.
t = (1, 2, ["red", "white"])
t[2][1] = "purple"
print t

t[2] returns the list, so the second line is equivalent to:

lst = t[2]
lst[1] = "purple"

That is exactly how you would access and change a list inside a list,
e.g.:

[1, 2, ["red", "white"]]
And which is better on resources....I' m guessing a tuple seeing
as it doesn't change.
I doubt it matters much unless you have several million lists v.
several million tuples. The reason you need to know about tuples is
because they are returned by some built in functions. Also, a key in
a dictionary can be a tuple, but not a list. But using a tuple as a
key in a dictionary is probably something you will never do.

Now there comes append. I read everywhere that append only add's 1
element to the end of your list. But if you write:
>>my_list = [1, 2, 3, 4, 5, 6]
my_list.appen d([7, 8, 9, 10])
my_list _li
[1, 2, 3, 4, 5, 6, [7, 8, 9, 10]]
Is that because list's, no matter what they contain, are counted as
1 element?
Each "element" in a list is associated with an index position. When
you append() to a list you are creating one additional index position
in the list and assigning the thing you append to that index
position. The thing you append becomes one element in the list, i.e.
it's associated with one index position in the list. As a result,
when you append(), the length of the list only increases by 1:
>>l = [1, 2]
len(l)
2
>>a = ["red", "white"]
l.append(a)
l
[1, 2, ['red', 'white']]
>>len(l)
3
>>>

And how would you sort the list that's in the list?
By summoning up the list, and then sorting it:

t = (1, 2, ["red", "white", "ZZZ", "abc"])
t[2].sort()
print t

Apr 12 '07 #3
And the last example brings up another question. What's the deal with a
tupple that has a list in it such as:
>>my_tupple = (1, 2, 3, 4, 5, [6, 7, 8, 9])

Now I read somewhere that you could change the list inside that tupple. But
I can't find any documentation that describes HOW to do it. The only things
I CAN find on the subject say, "Don't do it because its more trouble than
it's worth." But that doesn't matter to me, because I want to know
everything.
You could change the list inside your tuple like this:
>>my_tupple = (1, 2, 3, 4, 5, [6, 7, 8, 9])
my_tupple[5].append(10)
my_tupple
(1, 2, 3, 4, 5, [6, 7, 8, 9, 10])
Now there comes append. I read everywhere that append only add's 1 element
to the end of your list. But if you write:
>my_list = [1, 2, 3, 4, 5, 6]
my_list.append ([7, 8, 9, 10])
my_list
[1, 2, 3, 4, 5, 6, [7, 8, 9, 10]]

Is that because list's, no matter what they contain, are counted as 1
element?
Yes.
And how would you sort the list that's in the list? I guess that goes in
conjunction with the section above, but still:
>my_list = [6, 4, 3, 5, 2, 1]
my_list.append ([7, 9, 8, 10])
my_list.sort ()
my_list
[1, 2, 3, 4, 5, 6, [7, 9, 8, 10]]
How about:
>>my_list = [6, 4, 3, 5, 2, 1]
my_list.appen d([7, 9, 8, 10])
my_list[6].sort()
my_list
[6, 4, 3, 5, 2, 1, [7, 8, 9, 10]]
HTH,
Daniel
Apr 12 '07 #4
"Scott" <s_*********@co mcast.netwrites :
I'm going to start grouping all my questions in one post as this is
my second today, and sorta makes me feel dumb to keep having to
bother you all with trivial questions.
No, please don't do that. Separate questions leading to separate
discussions should have separate threads. Post them as separate
messages, each with a well-chosen Subject field for the resulting
thread.

--
\ "Why should I care about posterity? What's posterity ever done |
`\ for me?" -- Groucho Marx |
_o__) |
Ben Finney
Apr 12 '07 #5
En Thu, 12 Apr 2007 19:38:55 -0300, Scott <s_*********@co mcast.net>
escribió:
List's and Tuple's
I don't see the distinction between the two. I mean, I understand that a
list is mutable and a tuple is immutable.
The thing that I dont understand about them is what, besides that,
seperates
the two.
Perhaps this old post from 2001 can explain a bit:
http://groups.google.com/group/comp....e78f179a893526
Or perhaps this one from 1998:
http://groups.google.com/group/comp....199e16f119a020
Now you can add to a list, but not a tuple so:
>>>my_list.appe nd(my_tuple) #or extend for that matter right?
[1, 2, 3, 4, 5, 6, (1, 2, 3, 4, 5, 6)]

Is that pretty much accurate? And which is better on resources....I' m
guessing a tuple seeing as it doesn't change.
Yes. Tuples are immutable - once created, they can't change.
And the last example brings up another question. What's the deal with a
tupple that has a list in it such as:
>>>my_tupple = (1, 2, 3, 4, 5, [6, 7, 8, 9])

Now I read somewhere that you could change the list inside that tupple.
But
I can't find any documentation that describes HOW to do it. The only
things
I CAN find on the subject say, "Don't do it because its more trouble than
it's worth." But that doesn't matter to me, because I want to know
everything.
The *contents* of the list can be changed, but not the list itself:

my_tupple[5].append(10)
del my_tupple[5][2]

my_tupple will always contain *that* list, whatever you put inside it.
(Do not confuse the list object -a container- with the objects contained
inside it)
Now there comes append. I read everywhere that append only add's 1
element
to the end of your list. But if you write:
>>>my_list = [1, 2, 3, 4, 5, 6]
my_list contains 6 elements: len(my_list)==6
>>>my_list.appe nd([7, 8, 9, 10])
my_list
[1, 2, 3, 4, 5, 6, [7, 8, 9, 10]]
my_list now contains 7 elements: len(my_list)==7
Its seventh element happens to be a list itself, but that doesn't matter:
my_list sees it as a single object like any other.
Is that because list's, no matter what they contain, are counted as 1
element?
Exactly. Lists or whatever object you want, if you append it to my_list,
my_list grows by one element. It doesn't care *what* it is - it's a new
element.
And how would you sort the list that's in the list? I guess that goes in
conjunction with the section above, but still:
>>>my_list = [6, 4, 3, 5, 2, 1]
my_list.appe nd([7, 9, 8, 10])
my_list.sort ()
my_list
[1, 2, 3, 4, 5, 6, [7, 9, 8, 10]]
To sort my_list, you call the sort method on my_list: my_list.sort()
To sort "the list that's in the list", i.e. my_list[6], you call the sort
method on "the list that's in the list": my_list[6].sort()
This is, again, something I'm finding nothing on.
You call a method on any object using any_object.meth od_name(some,
parameters, may_be=required )
any_object may be any arbitrary expression, like my_list[6] above
Maybe I'm just not looking in the right spots. The only things I have as
learning aids are: this newsgroup ;p, http://diveintopython.org,
http://python.org/, Beggining Python: From Novice to Professional, and
(now
don't laugh) Python for Dummies.
That's fine - just keep programming, and have fun.

--
Gabriel Genellina

Apr 12 '07 #6
Yes. Tuples are immutable - once created, they can't change.

Just to explain that statement a little better. If you do this:
t = (1, 2, ["red", "white"])
t[2].append("purple ")
print t #(1, 2, ['red', 'white', 'purple'])
It sure looks like t changed, and therefore t is NOT immutable--and
the whole "tuples are immutable" mantra is a lie. However, the list
itself isn't actually stored inside t. What's stored inside t is
python's internal id for the list. So suppose python gave the list
the internal id: 10008. The tuple really looks like this:

t = (1, 2, <10008>)

And no matter what you do to the list: append() to it, sort() it,
etc., the list's id remains the same. In that sense, the tuple is
immutable because the id stored in the tuple never changes.

In actuality, the numbers 1 and 2 aren't stored in the list either--
python has internal id's for them too, so the tuple actually looks
like this:

t = (<56687>, <93413>, <10008>)
| | |
| | |
| | |
V V V
1 2 ["red", "white", "purple"]
Because of that structure, you can create situations like this:
>>lst = ["red", "white"]
t1 = (1, 2, lst)
t2 = (15, 16, lst)
print t1
(1, 2, ['red', 'white'])
>>print t2
(15, 16, ['red', 'white'])
>>lst.append("p urple")
print lst
['red', 'white', 'purple']
>>print t1
(1, 2, ['red', 'white', 'purple'])
>>print t2
(15, 16, ['red', 'white', 'purple'])
>>>
lst, t1, and t2 all refer to the same list, so when you change the
list, they all "see" that change. In other words, the names lst,
t1[2], and t2[2] all were assigned the same python id for the original
list ["red", "white"]. Since all those names refer to the same list,
any of those names can be used to change the list.

Apr 13 '07 #7
On Apr 12, 5:38 pm, "Scott" <s_brosci...@co mcast.netwrote:
I'm going to start grouping all my questions in one post as this is my
second today, and sorta makes me feel dumb to keep having to bother you all
with trivial questions. I'll just seperate my questions with:
---------------------------------------------------------------------------*----------------
Now onto the issue:
List's and Tuple's
I don't see the distinction between the two. I mean, I understand that a
list is mutable and a tuple is immutable.
The thing that I dont understand about them is what, besides that, seperates
the two. I did a little experimentation to try to understand it better, but
only confused myelf more.

A list looks like this:
>>my_list = [1, 2, 3, 4, 5, 6]

and a tuple looks like this:
>>my_tuple = (1, 2, 3, 4, 5, 6)

Now you can add to a list, but not a tuple so:
>>my_list.appen d(my_tuple) #or extend for that matter right?

[1, 2, 3, 4, 5, 6, (1, 2, 3, 4, 5, 6)]

Is that pretty much accurate? And which is better on resources....I' m
guessing a tuple seeing as it doesn't change.

And the last example brings up another question. What's the deal with a
tupple that has a list in it such as:
>>my_tupple = (1, 2, 3, 4, 5, [6, 7, 8, 9])

Now I read somewhere that you could change the list inside that tupple. But
I can't find any documentation that describes HOW to do it. The only things
I CAN find on the subject say, "Don't do it because its more trouble than
it's worth." But that doesn't matter to me, because I want to know
everything.
---------------------------------------------------------------------------*------------------

Now there comes append. I read everywhere that append only add's 1 element
to the end of your list. But if you write:>>my_list = [1, 2, 3, 4, 5, 6]
>my_list.append ([7, 8, 9, 10])
my_list

[1, 2, 3, 4, 5, 6, [7, 8, 9, 10]]

Is that because list's, no matter what they contain, are counted as 1
element?
Right, but I didn't see the following mentioned elsewhere, so note
that:

What you probably wanted to use to add [7,8,9,10] to your list was
..extend() not .append().
>>my_list = [1, 2, 3, 4, 5, 6]
my_list.exten d([7, 8, 9, 10])
my_list
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>
And how would you sort the list that's in the list? I guess that goes in
conjunction with the section above, but still:>>my_list = [6, 4, 3, 5, 2, 1]
>my_list.append ([7, 9, 8, 10])
my_list.sort ()
my_list

[1, 2, 3, 4, 5, 6, [7, 9, 8, 10]]

This is, again, something I'm finding nothing on.
---------------------------------------------------------------------------*------------------

Maybe I'm just not looking in the right spots. The only things I have as
learning aids are: this newsgroup ;p,http://diveintopython.org,http://python.org/, Beggining Python: From Novice to Professional, and (now
don't laugh) Python for Dummies.

Apr 13 '07 #8
Scott wrote:
Now I read somewhere that you could change the list inside that tupple. But
I can't find any documentation that describes HOW to do it. The only things
I CAN find on the subject say, "Don't do it because its more trouble than
it's worth." But that doesn't matter to me, because I want to know
everything.
Python 2.4.4c1 (#2, Oct 11 2006, 21:51:02)
[GCC 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu5)] on linux2
Type "help", "copyright" , "credits" or "license" for more information.
>>aa = (1, 2, [3, 4])
aa
(1, 2, [3, 4])
>>aa[2].append ((True, False))
aa
(1, 2, [3, 4, (True, False)])
>>>
Like that.
Mel.

Apr 13 '07 #9
On Apr 12, 5:38 pm, "Scott" <s_brosci...@co mcast.netwrote:
I'm going to start grouping all my questions in one post as this is my
second today, and sorta makes me feel dumb to keep having to bother you all
with trivial questions.
You might also investigate the python tutorial mailing list, which
covers many of these basic topics without seeming to be bothered by
much of anything.

-- Paul

Apr 13 '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
42
2716
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 a tuple? I understand that a tuple is immutable and a list is mutable but there has to be more to it than just that. Everything I tried with a list worked the same with a tuple. So, what's the difference and why choose one over the other? Jeff
2
1392
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 -- putting a collection of data in a tuple, when possible, makes it easier to follow the code. You do not have to think about whether contents have changed from where the tuple was first defined. Is "use tuples instead of lists, when possible"...
3
1504
by: mr_vocab | last post by:
hey what is the many dif with them why bothere using tuples and how do you edit a list??? thanks
1
1210
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 the right track with the logic I'm asking for hints again here at the list because i think i'm already into premature optimization...
1
1679
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 item. (Actually, it's a generator, and I use the set class outside of it to collect the unique items, but you get the idea. ;-) ) data = , , ,
10
1748
by: Wildemar Wildenburger | last post by:
Hi there :) I don't know how else to call what I'm currently implementing: An object that behaves like a list but doesn't store it's own items but rather pulls them from a larger list (if they match a certain criterion). Changes to the filter are instantly reflected in the underlying list. Clear enough? Ok, so I figured that this is generic enough to be found in some standard module already (I've had this often enough: Painfully
12
4149
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 like this: ('eco', "(u'Roads',)", 0.073969887301348305) Pysqlite doesn't like the format of the middle term: pysqlite2.dbapi2.InterfaceError: Error binding parameter 1 - probably
27
1665
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
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...
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...
0
7024
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
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.
0
1403
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.