2

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 ?

Georgy
  • 12,464
  • 7
  • 65
  • 73
roygbiv
  • 339
  • 3
  • 18
  • You need to create an empty list before a for-loop to iterate through your mylist. Then, you use an if conditional to append each iteration with len(4) into your empty list. – K.Land_bioinfo May 15 '20 at 23:32
  • 1
    @K.Land_bioinfo Yes but it would be very computational demanding in general case, i thought there could be something more efficient – roygbiv May 15 '20 at 23:33
  • I understand. I'm still fairly new to programming, so I am not the best at tips and tricks to making code more efficient yet. – K.Land_bioinfo May 15 '20 at 23:39
  • @Georgy it was similar and the anwser I accepted is more specific to my problem. There is no need to close the question – roygbiv May 21 '20 at 14:20
  • @roygbiv It's normal practice to close a question as a duplicate of a broader one. There is nothing bad about that. Your question will not be deleted and will stay as a signpost pointing people towards the canonical duplicate target. For more info see [Why are some questions marked as duplicate?](https://stackoverflow.com/help/duplicates), [How should duplicate questions be handled?](https://meta.stackexchange.com/q/10841/378331). – Georgy May 21 '20 at 14:59

6 Answers6

6

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

awarrier99
  • 3,628
  • 1
  • 12
  • 19
5

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.

xilpex
  • 3,097
  • 2
  • 14
  • 45
3

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']
Abbas
  • 623
  • 4
  • 6
1

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)
1

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)
K.Land_bioinfo
  • 170
  • 1
  • 3
  • 12
1
'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)
Juan Diego Lozano
  • 989
  • 2
  • 18
  • 30