w3resource

Python: Find the values of length six in a given list using Lambda


Write a Python program to filter a given list to determine if the values in the list have a length of 6 using Lambda.

Sample Solution:

Python Code :

# Create a list 'weekdays' containing the names of the days of the week
weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']

# Use the 'filter()' function with a lambda function to filter days with a length of 6 characters
# The lambda function checks the length of each day and keeps only the days with a length of 6
# Empty strings ('') are filtered out to keep only days with a length of 6 characters
days = filter(lambda day: day if len(day) == 6 else '', weekdays)

# Iterate through the filtered days and print each day that has a length of 6 characters
for d in days:
    print(d)

Sample Output:

Monday
Friday
Sunday

Python Code Editor:

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

Previous: Write a Python program to count the even, odd numbers in a given array of integers using Lambda.
Next: Write a Python program to add two given lists using map and 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.