Z. K
Z. K

Reputation: 7

How to change list of list to a string

I need to get:

'W1NaCl U1NaCl V1NaCl'

from:

[['W1NaCl'], ['U1NaCl'], ['V1NaCl']]

How to get required output in pythonic way

Upvotes: 0

Views: 42

Answers (2)

Tomer Ariel
Tomer Ariel

Reputation: 1537

items = [['W1NaCl'], ['U1NaCl'], ['V1NaCl']]
res = " ".join([item[0] for item in items])

Which yields: W1NaCl U1NaCl V1NaCl

Upvotes: 1

Benoît P
Benoît P

Reputation: 3265

You can do:

[[i] for i in 'W1NaCl U1NaCl V1NaCl'.split()]

Split will chop it into words, and the list comprehension will make the inner-arrays

Upvotes: 0

Related Questions