Newbie getting confused again

Peter Hansen peter at engcorp.com
Fri Mar 4 21:31:35 EST 2005


It's me wrote:
> If I have:
> 
>     a = (1,2,3)

Note that this is a tuple.

> how do I ended up with:
> 
>     res=[(1), (2), (3), (4), (5)]

Not that this is a list.  The two aren't the same thing.
If you don't understand the difference, you might want
to review the tutorial or head over to the tutor list.

Also note that (1) is just the same as 1, so what you
show above is the same as res=[1, 2, 3, 4, 5].  Is that
what you wanted?

> without doing:
> 
>     res=[(a[0]), (a[1]), (a[2]), (4), (5)]

Presumably what you are asking is how you can create
a new list (or tuple?) based on the contents of the
original tuple "a" but with two more elements added?

This will do precisely what you've described, though
it does convert "a" from a tuple into a list because
you couldn't concatenate (join together) the two objects
otherwise:

    res = list(a) + [4, 5]


-Peter



More information about the Python-list mailing list