Pryza Pryza - 7 months ago 23
Python Question

Convert a list into a string and allow for a separator

please be aware, im new to python:
i'm trying to create a defined function that can convert a list into a string, and allows me to put a separator in. The separator has to be ', '.
My current thought process is to add each item from a list to an empty string variable, and then I'm trying to make use of the range function to add a separator in. I'm only wanting to use str() and range().

def list2Str(lisConv, sep = ', '):
var = ''
for i in lisConv:
var = var + str(i)
#test line
print(var, "test line")
var1 = int(var)
for a in range(var1):
print(str(var1)[a], sep = ', ')

list1 = [2,0,1,6]

result = list2Str(list1, ', ')

list=['asdf', '123', 'more items...']
print ', '.join([str(x) for x in list])

If you wanted to create your own function to convert you could do the following.

def convert(list, sep):
    n_str = ''
    for i in list:
        n_str += str(i) + sep
    return n_str