My site

Converting a list to a string in python


Here I am converting a list of strings to a single string and placing a comma between each item in the new string.

1
2
3
4
5
6
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# convert list to string with removed []
LIST = ['first', 'second', 'third', 'fourth']
str_list = (", ".join(LIST))

This is the output:

print str_list
>> 'first, second, third, fourth