Merging all elements in a list into one single element

Merging all elements in a list into one single element

Problem Description:

I’m trying to merge elements in a list, into one single element back into the list.

For instance I have a list:

fruits = ['apple', 'orange', 'grape']

How do I merge all of them to become, let’s say:

fruits = ['apple orange grape']

or

fruits = ['appleorangegrape']

or even

fruits ['apple, orange, grape']

Thank you in advance!!

So far thought about concat(), but it works for list to list, not for combining elements.

Solution – 1

fruits = ['apple', 'orange', 'grape']

fruits_str = ' '.join(fruits)
print(fruits_str) # for 'apple orange grape'

fruits_str = ''.join(fruits)
print(fruits_str)  # for 'appleorangegrape'

fruits_str = ', '.join(fruits)
print(fruits_str)  # for 'apple, orange, grape'
Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject