90abyss 90abyss - 18 days ago 6
Python Question

Remove square brackets from list

I have this list:

list1 = [['123'], ['456'], ['789']]

I want to convert this list into a string and later store it into a column in a database in this form:

123 / 456 / 789

I tried doing this:

s2 = ", ".join(repr(e) for e in list1)

But this is what I'm getting:

['123'], ['456'], ['789']

Any ideas on what should I do next to get the desired output?


You are close, but what you want to do is flatten your list of lists first, then convert to string. Like this:

" / ".join([item for sublist in list1 for item in sublist])