Lists and Tuples and Much More

Daniel Nogradi nogradi at gmail.com
Thu Apr 12 19:13:19 EDT 2007


> 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.append([7, 9, 8, 10])
>>> my_list[6].sort()
>>> my_list
[6, 4, 3, 5, 2, 1, [7, 8, 9, 10]]


HTH,
Daniel



More information about the Python-list mailing list