w3resource

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

Python Lambda: Exercise-14 with Solution

Write a Python program to find the values of length six in a given list using Lambda.

Sample Solution:

Python Code :

weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
days = filter(lambda day: day if len(day)==6 else '', weekdays)
for d in days:
  print(d)

Sample Output:

Monday
Friday
Sunday

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:


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 Python skills with w3resource's quiz


Python: Tips of the Day

Flattens a list, by spreading its elements into a new list

Example:

def tips_spread(arg):
  ret = []
  for i in arg:
    ret.extend(i) if isinstance(i, list) else ret.append(i)
  return ret

print(tips_spread([2, 4, 6, [1, 3, 5], [7], 8, 9]))

Output:

[2, 4, 6, 1, 3, 5, 7, 8, 9]