Lists and Tuples and Much More

mensanator at aol.com mensanator at aol.com
Thu Apr 12 20:46:26 EDT 2007


On Apr 12, 5:38 pm, "Scott" <s_brosci... at comcast.net> wrote:
> 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.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.
>
> 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.extend([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.





More information about the Python-list mailing list