FGem
FGem

Reputation: 3

Print list separated in a single row in python

I've got

data = list(a.fetchall())
print data

It returns:

[('Data1',), ('Data2',), ('Data3',), ('Data4',), ('Data5',), ('Data6',), ('Data7 ',), ('Data8',)]

How could I get those values separated?

I tried to use

print ', '.join(data)

But couldn't get a good return of values.

The return should be like

data1,data2,data3...

And can save the data in different variables if possible...

Upvotes: 0

Views: 70

Answers (4)

PM 2Ring
PM 2Ring

Reputation: 55469

Here's a functional solution.

from operator import itemgetter

data = [
    ('Data1',), ('Data2',), ('Data3',), ('Data4',), 
    ('Data5',), ('Data6',), ('Data7 ',), ('Data8',)
]
# Extract the string from each tuple into a new list
newdata = map(itemgetter(0), data)
# Join the strings in the list into a single string
print newdata
print ', '.join(newdata)

output

['Data1', 'Data2', 'Data3', 'Data4', 'Data5', 'Data6', 'Data7 ', 'Data8']
Data1, Data2, Data3, Data4, Data5, Data6, Data7 , Data8

Upvotes: 1

Ersel Er
Ersel Er

Reputation: 771

Try this

[elem[0] for elem in data]

Upvotes: 0

marmeladze
marmeladze

Reputation: 6564

', '.join(map(lambda e: e[0], data))

Upvotes: 1

Daniel Roseman
Daniel Roseman

Reputation: 599520

You have a list of one-item tuples. You need to extract the values from the tuples themselves.

', '.join(element[0] for element in data)

Upvotes: 1

Related Questions