-2

How can I create a Python custom function to get list of strings having length more than a number n when we pass number n and list of strings?

I tried using this function but it returns None:

lst=['shiva', 'patel', 'ram','krishna', 'bran']
filtered_list=[]
def word_remove(n, lst):
    for i in lst:
        if len(i)>n:
            return filtered_list.append(i)
print(word_remove(4,lst))

The output is :

None
Rafid Aslam
  • 305
  • 3
  • 10

3 Answers3

1

append method on a list does not return any value. Hence the None type.

MjZac
  • 3,476
  • 1
  • 17
  • 28
0

Append function has no return type. Try instead this.

def word_remove(n, lst):
    for i in lst:
        if len(i) > n:
            filtered_list.append(i)
    return filtered_list
mhhabib
  • 2,975
  • 1
  • 15
  • 29
0
lst = ['shiva', 'patel', 'ram','krishna', 'bran']

filtered_list=[]

def word_remove(n, lst):
    for i in lst:
        if len(i)>n:
            filtered_list.append(i)
    return filtered_list

print (word_remove(4,lst))

Output:

['shiva', 'patel', 'krishna']

The method append does not return anything. So you need to return the whole filtered_list.

Synthase
  • 5,849
  • 2
  • 12
  • 34