Newbie getting confused again

John Machin sjmachin at lexicon.net
Sat Mar 5 03:03:00 CET 2005


It's me wrote:
> If I have:
>
>     a = (1,2,3)
>
> how do I ended up with:
>
>     res=[(1), (2), (3), (4), (5)]
>
> without doing:
>
>     res=[(a[0]), (a[1]), (a[2]), (4), (5)]
>

If by (x) you really mean a tuple with 1 element i.e. (x,) then you
need something like this:

>>> a = (1, 2, 3)
>>> b = [(4,), (5,)]
>>> res = [tuple([x]) for x in a] + b
>>> res
[(1,), (2,), (3,), (4,), (5,)]
>>>

Otherwise (a simpler requirement):

>>> a = (1, 2, 3)
>>> b = [4, 5]
>>> res = list(a) + b
>>> res
[1, 2, 3, 4, 5]
>>>

If NEITHER of the above is what you want, you'll need to try being a
bit clearer about what you are trying to achieve.

And a tip: if "a" is really of variable length, you should make it a
list, not a tuple; read the FAQ.

HTH,
John




More information about the Python-list mailing list