w3resource

Python: Reverse strings in a given list of string values using lambda


41. Reverse Strings Lambda

Write a Python program to reverse strings in a given list of string values using lambda.

Sample Solution:

Python Code :

# Define a function 'reverse_strings_list' that reverses each string in a list
def reverse_strings_list(string_list):
    # Use 'map' function with a lambda to reverse each string in 'string_list' and join the characters back together
    result = list(map(lambda x: "".join(reversed(x)), string_list))
    
    # Return the list with reversed strings
    return result

# Create a list 'colors_list' containing strings representing different colors
colors_list = ["Red", "Green", "Blue", "White", "Black"]

# Print the original list 'colors_list'
print("\nOriginal list:")
print(colors_list)

# Print the list with reversed strings using the 'reverse_strings_list' function
print("\nReverse strings of the said given list:")
print(reverse_strings_list(colors_list)) 

Sample Output:

Original lists:
['Red', 'Green', 'Blue', 'White', 'Black']

Reverse strings of the said given list:
['deR', 'neerG', 'eulB', 'etihW', 'kcalB']

For more Practice: Solve these Related Problems:

  • Write a Python program to reverse only the vowels in each string of a list using lambda.
  • Write a Python program to reverse each word in a sentence while keeping the word order intact using lambda.
  • Write a Python program to reverse the order of characters in each string, excluding punctuation, using lambda.
  • Write a Python program to reverse each string in a list and then sort the list lexicographically using lambda.

Python Code Editor:

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

Previous: Write a Python program to find the nested lists elements, which are present in another list using lambda.
Next: Write a Python program to calculate the product of a given list of numbers using lambda.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.