Lists and Tuples and Much More
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Thu Apr 12 19:26:53 EDT 2007
En Thu, 12 Apr 2007 19:38:55 -0300, Scott <s_broscious at comcast.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.lang.python/browse_thread/thread/7eaf9fe92b4c7e47/#78e78f179a893526
Or perhaps this one from 1998:
http://groups.google.com/group/comp.lang.python/browse_thread/thread/358ef18309812fbb/14199e16f119a020
> Now you can add to a list, but not a tuple so:
>
>>>> my_list.append(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.append([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.append([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.method_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
More information about the Python-list
mailing list