Bob
Bob

Reputation: 10795

words in the list in Python

How can I get a one word school from the list ['s', 'c', 'h', 'o', 'o', 'l']?

Thanks!

Upvotes: 0

Views: 153

Answers (5)

Anuj
Anuj

Reputation: 9622

Hope this would help you . Even though ''.join(['s', 'c', 'h', 'o', 'o', 'l']) works I personally love using the method shown below .. Anyway its ur call :)

>>> word
['s', 'c', 'h', 'o', 'o', 'l']
>>> reduce(lambda a,x:a+x,word)
'school'
>>> 

Upvotes: 0

the wolf
the wolf

Reputation: 35522

>>> l=['s', 'c', 'h', 'o', 'o', 'l']
>>> print ''.join(l)
school

Upvotes: 1

Serkan
Serkan

Reputation: 1207

must have been asked/answered a million times, but here it is:

''.join(['s', 'c', 'h', 'o', 'o', 'l'])

Upvotes: 2

Uku Loskit
Uku Loskit

Reputation: 42040

your_list = ['s', 'c', 'h', 'o', 'o', 'l']
new_string = "".join(your_list)

Upvotes: 1

user395760
user395760

Reputation:

str.join(iterable)

Return a string which is the concatenation of the strings in the iterable iterable. A TypeError will be raised if there are any non-string values in seq, including bytes objects. The separator between elements is the string providing this method.

I.e. ''.join(parts)

As indicated in the description, this works for all iterables of strings, not just for lists of characters (single-letter strings).

Upvotes: 9

Related Questions