Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I turn this function into Lambda?

I’m trying to turn this function into a one line function for assignment purposes.

def counter(list_to_count, char_to_count):
    ocurrences_list = []
    for i in list_to_count:
        x = i.count(char_to_count)
        ocurrences_list.append(x)
    return ocurrences_list

This function takes a list of strings and counts the ocurrences of "char" in each element, then makes a list with the amount of ocurrences of "char" per element.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use a list comprehension:

lambda list_to_count, char_to_count: [i.count(char_to_count) for i in list_to_count]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading