Python concatenate/joining list items

 

By using join function we can concatenate or join all items with a delimiter(all items in list must strings) in a list.

I have a list as [‘a’, ‘b’, ‘c’] and joining each value with space delimiter in following example:

>>> list = ['a', 'b', 'c']
>>> " ".join(list)
'a b c'

To join with comma delimiter:

>>> ",".join(list)
'a,b,c'

Similarly we can use join or concatenate all values in list. The only condition here is all values in list must strings.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s