-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
  • 187
  • 1
  • 9

3 Answers3

1

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

MjZac
  • 3,303
  • 1
  • 20
  • 27
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,733
  • 1
  • 11
  • 22
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
  • 4,920
  • 2
  • 9
  • 31