w3resource

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

Python Lambda: Exercise-31 with Solution

Write a Python program to extract specified size of strings from a given 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

Inverts a dictionary with unique hashable values:

Example:

def tips_invert_dictionary(obj):
  return { value: key for key, value in obj.items() }
ages = {
  "Owen": 29,
  "Eddie": 15,
  "Jhon": 22,
}
print(tips_invert_dictionary(ages))

Output:

{29: 'Owen', 15: 'Eddie', 22: 'Jhon'}