Computer >> Computer tutorials >  >> Programming >> Python

Python - Join tuple elements in a list


In this article, we are going to learn how to join tuple elements in a list. It's a straightforward thing using join and map methods. Follow the below steps to complete the task.

  • Initialize list with tuples that contain strings.
  • Write a function called join_tuple_string that takes a tuple as arguments and return a string.
  • Join the tuples in the list using map(join_tuple_string, list) method.
  • Convert the result to list.
  • Print the result.

Example

# initializing the list with tuples
string_tuples = [('A', 'B', 'C'), ('Tutorialspoint', 'is a', 'popular', 'site', 'for tech learnings')]

# function that converts tuple to string
def join_tuple_string(strings_tuple) -> str:
   return ' '.join(strings_tuple)

# joining all the tuples
result = map(join_tuple_string, string_tuples)

# converting and printing the result
print(list(result))

If you run the above code, then you will get the following result.

Output

['A B C', 'Tutorialspoint is a popular site for tech learnings']

Conclusion

If you have any queries in the article, mention them in the comment section.