Remove string shorter than k from a list of strings

Viewed 651

I have a list of strings like the following:

mylist = ['a', 'b', 'c', 'aa', 'bb', 'cc', 'aaa', 'bbb', 'ccc',  'aaaa', 'bbbb', 'cccc']

And I need to extract only the strings with k=4 characters, so the output would be:

minlist = ['aaaa', 'bbbb', 'cccc']

How can be implemented efficiently ?

6 Answers

This is exactly the type of situation the filter function is intended for:

>>> mylist = ['a', 'b', 'c', 'aa', 'bb', 'cc', 'aaa', 'bbb', 'ccc',  'aaaa', 'bbbb', 'cccc']
>>> minlist = list(filter(lambda i: len(i) == 4, mylist))
>>> minlist
['aaaa', 'bbbb', 'cccc']

filter takes two arguments: the first is a function, and the second is an iterable. The function will be applied to each element of the iterable, and if the function returns True, the element will be kept, and if the function returns False, the element will be excluded. filter returns the result of filtering these elements according to the passed in function

As a sidenote, the filter function returns a filter object, which is an iterator, rather than a list (which is why the explicit list call is included). So, if you're simply iterating over the values, you don't need to convert it to a list as it will be more efficient

Try this:

def get_minlist(my_list, k):
    return [item for item in my_list if len(item) == k]

You can use this as:

print(get_minlist(["abc", "ab", "a"], 2))

Result:

['ab']

The code is pythonic, fast, and is very easy to understand. The code goes through the items in the list, checks if they are k in length, if so it keeps them.

You can check the length of a string using len().

mylist = ['a', 'b', 'c', 'aa', 'bb', 'cc', 'aaa', 'bbb', 'ccc',  'aaaa', 'bbbb', 'cccc']
minlist = [x for x in mylist if len(x) == 4]

Result:

['aaaa', 'bbbb', 'cccc']

Try this:

mylist = ['a', 'b', 'c', 'aa', 'bb', 'cc', 'aaa', 'bbb', 'ccc',  'aaaa', 'bbbb', 'cccc']
minilist=[]

for i in range (len(mylist)):
    if len(mylist[i]) == 4:
        minilist.append(mylist[i])

print(minilist)

Like I said in the comment, you could try something like this:

mylist = ['a', 'b', 'c', 'aa', 'bb', 'cc', 'aaa', 'bbb', 'ccc',  'aaaa', 'bbbb', 'cccc']

newlst=[]
for item in mylist:
    if len(item) == 4:
        newlst.append(item)

print (newlst)
'mylist = ['a', 'b', 'c', 'aa', 'bb', 'cc', 'aaa', 'bbb', 'ccc',  'aaaa', 'bbbb', 'cccc']

here we are using the concept called as list comprehension,list comprehension means it is a easy way to create a list based on some iterables. note:-iterable is something which can be looped over during list comprehension creation elements from the iterables(ex:-mylist) can be conditionally included in the new list and transformed as needed

syntax of list comprehension:-

note:- this symbol '|' is used to tell syntax as three parts,1st 2 parts are mandatory and the last part is optional

[give me this | from the collection | with this condition ]

[mandatory    |  mandatory          |    optional ]

[var for var in iterables condition ] 

filtered_list=[item for item in mylist if len(item)==4]
print(filtered list)
Related