0

How to change list of tuples in list of str?

17th Nov 2021, 11:40 AM
Vira
Vira - avatar
5 odpowiedzi
+ 5
a =[("h", ), ("e", ), ("y", )] b = ''.join([tup[0] for tup in a]) # There's way too many different ways. Please include code because each solution may be different
17th Nov 2021, 12:10 PM
Slick
Slick - avatar
+ 3
Cut it using join. The double quote isn't really a character to strip, it's to denote that what's inside is a string. Knowing that and that tuples ( <val1>, [val2],...) are just containers like lists (not characters to strip either) there are several ways you can do it. Simon provided a great example that satisfies the requirements. Give his code a lookthough and test it out
18th Nov 2021, 12:49 PM
Slick
Slick - avatar
+ 2
# A different scenario: c = [("h", "e", "y", ), ("t", "h", "e", "r", "e", )] d = ["".join(tup) for tup in c] print(d) # As Slick said the details depend on what exactly you want to do.
17th Nov 2021, 1:27 PM
Simon Sauter
Simon Sauter - avatar
+ 2
I see. Thank you!
18th Nov 2021, 3:25 PM
Vira
Vira - avatar
0
Thank you for answers. I have a file with list of tuples and I should convert it in list of strings. I should write a function. I wrote something like this: def some_func(param): return " ".join(str(i).strip(")").strip("(") for i in param) param = [("h", "e", "y", ), ("t", "h", "e", "r", "e", )] print(some_func(param)) My problem was that I didn't know how to cut "()" maybe without strip()
18th Nov 2021, 11:06 AM
Vira
Vira - avatar