w3resource

Python: Extract specified size of strings from a give list of string values using lambda

Python Lambda: Exercise-31 with Solution

Write a Python program to extract specified size of strings from a give list of string values using lambda.

Sample Solution:

Python Code :

def extract_string(str_list1, l):
    result = list(filter(lambda e: len(e) == l, str_list1))
    return result

str_list1 = ['Python', 'list', 'exercises', 'practice', 'solution'] 
print("Original list:")
print(str_list1)
l = 8
print("\nlength of the string to extract:")
print(l)
print("\nAfter extracting strings of specified length from the said list:")
print(extract_string(str_list1 , l))

Sample Output:

Original list:
['Python', 'list', 'exercises', 'practice', 'solution']

length of the string to extract:
8

After extracting strings of specified length from the said list:
['practice', 'solution']

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 sort a given matrix in ascending order according to the sum of its rows using lambda.
Next: Write a Python program to count float number in a given mixed list using lambda.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Returns all the elements of a list except the last one

Example:

def tips_initial(lst):
  return lst[0:-1]

print(tips_initial([1, 2, 3, 4]))

Output:

[1, 2, 3]