user3235542
user3235542

Reputation:

manipulating list of strings in python

How can I obtain the expected result below?

data = ['abc', 'def', 'ghi']

Expected result is:

 [abc, def, ghi]

My attempt is:

ans = [', '.join([''.join(i) for i in data])]
print ans 
['abc, def, ghi']

Upvotes: 0

Views: 62

Answers (2)

idiot.py
idiot.py

Reputation: 388

You could convert the list into a string, then use replace to remove the quotation marks.

data = ['abc', 'def', 'ghi']
ans = str(data).replace('\'', '')
print(ans)

Output:

[abc, def, ghi]

Simples!

Upvotes: 1

Tok Soegiharto
Tok Soegiharto

Reputation: 329

Try below code:

>>> ans = '[' + ', '.join([''.join(i) for i in data]) + ']'
>>> ans
'[abc, def, ghi]'
>>> 

Upvotes: 1

Related Questions