Pendle S
Pendle S

Reputation: 103

Python - How to output strings that are in a list, that have a certain number of letters in them

Using Python 3.7, I have a list that contains strings of various lengths. I'm trying to use functions to only return strings that have two letters - my threshold. I am currently getting a single string output of "a", when I really want "a", "ab", and "ac" to be printed. I do not know where I am going wrong? I know that len(xStr) will count the number of letters in a string, but I'm not sure how to use it correctly here.

This is my attempted code:

threshold = 2
def listOfWords(list):
    stringList = ["a", "ab", "abc", "ac", "abcd"]
    return stringList

def wordsInListsCounter():
    for elements in listOfWords(list):
        if len(elements) <= threshold:
            strLessThanThreshold = elements
            return strLessThanThreshold
        elif len(elements) == 0:
            emptyString = "There are no words in this list"
            return emptyString
        else:
            error = "There is invalid information"
            return error
print(wordsInListsCounter())

Any help would be appreciated!! I'm a newbie Python user here...

Upvotes: 1

Views: 81

Answers (2)

lyxal
lyxal

Reputation: 1118

Incorperating @kevh’s answer into your code would look like so:

threshold = 2

def listOfWords(list):
    stringList = ["a", "ab", "abc", "ac", "abcd"]
    return stringList

def wordsInListsCounter():
    elements = listOfWords(list)

    if len(elements) != 0:
         strLessThanThreshold = [x for x in elements if len(x) <= threshold]
         return strLessThanThreshold

    elif len(elements) == 0:
        emptyString = "There are no words in this list"
        return emptyString

    else:
        error = "There is invalid information"
        return error

print(wordsInListsCounter())

However, if you don’t want to use a list comprehension, you can use the following:

threshold = 2
def listOfWords(list):
    stringList = ["a", "ab", "abc", "ac", "abcd"]
    return stringList

def wordsInListsCounter():
    strLessThanThreshold = []
    elements = listOfWords(list)

    for element in elements :
        if len(element) <= threshold:
            strLessThanThreshold.append(element)

    if len(elements) == 0:
        emptyString = "There are no words in this list"
        return emptyString

    return strLessThanThreshold

print(wordsInListsCounter())

Upvotes: 1

kevh
kevh

Reputation: 323

Use a list comprehension:

>>> stringList = ["a", "ab", "abc", "ac", "abcd"]
>>> modifiedList = [x for x in stringList if len(x) <= 2]
>>> modifiedList
['a', 'ab', 'ac']

I've edited my answer to better match your question, here's what I would add:

threshold = 2
myList = ["a", "ab", "abc", "ac", "abcd"]

def wordsInListsCounter(stringList):
    elements = []
    for element in stringList:
        if len(element) <= threshold:
            elements.append(element)
    return elements

elements = wordsInListsCounter(myList)

if len(elements) == 0:
    print("There are no words in this list")

else:
    print(elements)

Upvotes: 1

Related Questions