Python: Reverse strings in a given list of string values using lambda
Python Lambda: Exercise-41 with Solution
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']
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.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/python-exercises/lambda/python-lambda-exercise-41.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics