I have a list and it adds each letter of a word one by one to this list, I don't know what will be in the list until the program is run. How do I join each letter in the list into one word? e.g. turn ['p', 'y', 't', 'h', 'o', 'n']
into ['python']
.
The most conventional method to concatenate lists in python is by using the concatenation operator(+). The “+” operator can easily join the whole list behind another list and provide you with the new list as the final output as shown in the below example.
To convert a list to a string, use Python List Comprehension and the join() function. The list comprehension will traverse the elements one by one, and the join() method will concatenate the list's elements into a new string and return it as output.
a = ['a', 'b', 'c']
res = "".join(a)
You can again convert back to list of letters using :
list(res)
''.join(str(v) for v in my_list)
Since you do not know what will be in the list
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With