Joining elements in a list to 1 element

Francesco Guerrieri f.guerrieri at gmail.com
Thu Aug 23 09:41:04 EDT 2007


On 8/23/07, J. Cliff Dyer <jcd at sdf.lonestar.org> wrote:
>
>  What do you want to have happen in this case?
>
> my_list = [ 'a', 4, 'c' ]
>


It depends on the requirements of the OP.
A possible solution could be:

my_second_list = []
try:
    my_second_list.append("".join(my_list))
except TypeError:
    my_second_list.append("".join(str(item) for item in my_list) )


But it depends on the the actual requirements, maybe it isn't meaningful for
the original list to contain numbers (think for instance of nucleotide
sequences...?)

Francesco
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070823/36b15415/attachment.html>


More information about the Python-list mailing list