Joining elements in a list to 1 element

dimitri pater dimitri.pater at gmail.com
Thu Aug 23 05:49:59 EDT 2007


Dear all,
I am having trouble joining elements in a list into 1 element.
e.g. ['a','b','c'] into ['abc'] so that len(list) returns 1

I have tried the following:
myList = ['a','b','c']
print myList
>>>['a', 'b', 'c']
# get type
print type(myList)
>>><type 'list'>
# get length
print len(myList)
>>>3

myList2 = ''.join(myList)
print myList2
>>>abc
# get type
print type(myList2)
>>><type 'str'>
# get length
print len(myList2)
>>>3

As you can see, the type 'list' turns into 'str' after .join. Doing
something like list(''.join(myList)) returns the type 'list' but still has 3
elements.

thanks,
Dimitri

-- 
---
You can't have everything. Where would you put it? -- Steven Wright
---
please visit www.serpia.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070823/d6557dc1/attachment.html>


More information about the Python-list mailing list