w3resource

Python: Sum the length of the names of a given list of names after removing the names that starts with a lowercase letter

Python Lambda: Exercise-22 with Solution

Write a Python program that sums the length of a list of names after removing those that start with lowercase letters. Use the lambda function.

Sample Solution:

Python Code :

# Create a list of strings named 'sample_names' containing various names
sample_names = ['sally', 'Dylan', 'rebecca', 'Diana', 'Joanne', 'keith']

# Filter the 'sample_names' list using a lambda function to include only names that start with an uppercase letter
# followed by lowercase letters for the rest of the name
sample_names = list(filter(lambda el: el[0].isupper() and el[1:].islower(), sample_names))

# Print a label for the upcoming result
print("Result:")

# Calculate the total length of the concatenated string formed by joining all the filtered names together
print(len(''.join(sample_names))) 

Sample Output:

Result:
16

Python Code Editor:

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

Previous: Write a Python program that multiply each number of given list with a given number using lambda function. Print the result.
Next: Write a Python program to calculate the sum of the positive and negative numbers of a given list of numbers using lambda function.

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.