3

I want to make list data to string. My list data like this :

[['data1'],['data2'],['data3']]

I want to convert to string like this :

"[data1] [data2] [data3]"

I try to use join like this :

data=[['data1'],['data2'],['data3']]
list=" ".join(data)

But get error like this :

  string= " ".join(data)
TypeError: sequence item 0: expected string, list found

Can somebody help me?

8
  • Are you aware that you have nested lists (a list that contains lists that contain strings), not a simple list of strings? Commented Oct 8, 2015 at 4:12
  • @TigerhawkT3 I get the list from read CSV File and get that format. Commented Oct 8, 2015 at 4:15
  • That's fine. Are you aware that data does not contain strings? In fact, it contains lists. You can join strings, but not lists. Try another approach with that fact in mind. Commented Oct 8, 2015 at 4:17
  • @TigerhawkT3 can you help me with the simple code? Sorry, I'm newbie in Python programming Commented Oct 8, 2015 at 4:19
  • @ImamAbdulMahmudi Can there be more than one element in the sublist? If so, how do you want to handle that? Commented Oct 8, 2015 at 4:19

7 Answers 7

1

Depending on how closely you want the output to conform to your sample, you have a few options, show here in ascending order of complexity:

>>> data=[['data1'],['data2'],['data3']]
>>> str(data)
"[['data1'], ['data2'], ['data3']]"
>>> ' '.join(map(str, data))
"['data1'] ['data2'] ['data3']"
>>> ' '.join(map(str, data)).replace("'", '')
'[data1] [data2] [data3]'

Keep in mind that, if your given sample of data doesn't match your actual data, these methods may or may not produce the desired results.

Sign up to request clarification or add additional context in comments.

Comments

1

Have you tried?

data=[['data1'],['data2'],['data3']]
t =  map(lambda x : str(x), data)
print(" ".join(t))

Live demo - https://repl.it/BOaS

2 Comments

Why the lambda x : str(x) rather than just str?
Oh yeah. You are right. Was trying different things, so forgot to take them out.
1

In Python 3.x , the elements of the iterable for str.join() has to be a string .

The error you are getting - TypeError: sequence item 0: expected string, list found - is because the elements of the list you pass to str.join() is list (as data is a list of lists).

If you only have a single element per sublist, you can simply do -

" ".join(['[{}]'.format(x[0]) for x in data])

Demo -

>>> data=[['data1'],['data2'],['data3']]
>>> " ".join(['[{}]'.format(x[0]) for x in data])
'[data1] [data2] [data3]'

If the sublists can have multiple elements and in your output you want those multiple elements separated by a , . You can use a list comprehension inside str.join() to create a list of strings as you want. Example -

" ".join(['[{}]'.format(','.join(x)) for x in data])

For some other delimiter other than ',' , use that in - '<delimiter>'.join(x) .

Demo -

>>> data=[['data1'],['data2'],['data3']]
>>> " ".join(['[{}]'.format(','.join(x)) for x in data])
'[data1] [data2] [data3]'

For multiple elements in sublist -

>>> data=[['data1','data1.1'],['data2'],['data3','data3.1']]
>>> " ".join(['[{}]'.format(','.join(x)) for x in data])
'[data1,data1.1] [data2] [data3,data3.1]'

Comments

1
>>> import re
>>> l = [['data1'], ['data2'], ['data3']]
>>> s = ""
>>> for i in l:
        s+= re.sub(r"\'", "", str(i))
>>> s
'[data1][data2][data3]'

Comments

0

How about this?

data = [['data1'], ['data2'], ['data3']]
result = " ".join('[' + a[0] + ']' for a in data)
print(result)

Comments

0

How about this:

In [13]: a = [['data1'],['data2'],['data3']]

In [14]: import json

In [15]: temp = " ".join([json.dumps(x) for x in a]).replace("\"", "")

In [16]: temp
Out[16]: '[data1] [data2] [data3]'

Comments

0

Try the following. This can also be achieved by "Reduce":

from functools import reduce
data = [['data1'], ['data2'], ['data3']]
print(list(reduce(lambda x,y : x+y, data)))

output: ['data1', 'data2', 'data3']

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.