Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tuple conversion to a string

Tags:

python

I have the following list:

[('Steve Buscemi', 'Mr. Pink'), ('Chris Penn', 'Nice Guy Eddie'), ...]

I need to convert it to a string in the following format:

"(Steve Buscemi, Mr. Pink), (Chris Penn, Nice Guy Eddit), ..."

I tried doing

str = ', '.join(item for item in items)

but run into the following error:

TypeError: sequence item 0: expected string, tuple found

How would I do the above formatting?

like image 618
David542 Avatar asked Dec 01 '22 05:12

David542


1 Answers

', '.join('(' + ', '.join(i) + ')' for i in L)

Output:

'(Steve Buscemi, Mr. Pink), (Chris Penn, Nice Guy Eddie)'
like image 92
ovgolovin Avatar answered Dec 06 '22 16:12

ovgolovin