0

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']
3
  • That looks fine what is the problem? Commented Jun 22, 2014 at 7:42
  • 3
    Maybe "[" + ", ".join(data) + "]" ? Commented Jun 22, 2014 at 7:45
  • Do you mean the output you expect is "[abc, def, ghi]"? You understand what the quote marks and brackets actually mean, yes? Commented Jun 22, 2014 at 8:06

2 Answers 2

1

Try below code:

>>> ans = '[' + ', '.join([''.join(i) for i in data]) + ']'
>>> ans
'[abc, def, ghi]'
>>> 
Sign up to request clarification or add additional context in comments.

2 Comments

@alps Just want to know if this is a correct answer, so i can improve my answer.
this does not match your expected output, what do you actually want as output?
1

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.