w3resource

Python: Convert a given list of tuples to a list of strings using map function

Python map: Exercise-17 with Solution

Write a Python program to convert a given list of tuples to a list of strings using map function.

Sample Solution:

Python Code:

def tuples_to_list_string(lst):
    result = list(map(' '.join, lst))
    return result   
colors = [('red', 'pink'), ('white', 'black'), ('orange', 'green')]
print("Original list of tuples:")
print(colors)
print("\nConvert the said list of tuples to a list of strings:")
print(tuples_to_list_string(colors))
names = [('Sheridan','Gentry'), ('Laila','Mckee'), ('Ahsan','Rivas'), ('Conna','Gonzalez')]
print("\nOriginal list of tuples:")
print(names)
print("\nConvert the said list of tuples to a list of strings:")
print(tuples_to_list_string(names))

Sample Output:

Original list of tuples:
[('red', 'pink'), ('white', 'black'), ('orange', 'green')]

Convert the said list of tuples to a list of strings:
['red pink', 'white black', 'orange green']

Original list of tuples:
[('Sheridan', 'Gentry'), ('Laila', 'Mckee'), ('Ahsan', 'Rivas'), ('Conna', 'Gonzalez')]

Convert the said list of tuples to a list of strings:
['Sheridan Gentry', 'Laila Mckee', 'Ahsan Rivas', 'Conna Gonzalez']

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to convert a given list of strings into list of lists using map function.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz


Python: Tips of the Day

Checks if a string is an anagram of another string (case-insensitive, ignores spaces, punctuation and special characters)

Example:

def tips_anagram(s1, s2):
  _str1, _str2 = s1.replace(" ", ""), s2.replace(" ", "")
  return False if len(_str1) != len(_str2) else sorted(_str1.lower()) == sorted(_str2.lower())

print(tips_anagram("TRIANGLE", "INTEGRAL"))
print(tips_anagram("anagram", "Nag a ram"))

Output:

True
True