Python Forum
Removing all strings in a list that are of x length
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Removing all strings in a list that are of x length
#4
How do you guys feel about a single-pass version? As we look for the longest item, we can yield items that we already know aren't that long. The performance can be dramatically improved if the order of the items doesn't matter (small items can be yielded immediately).

def remove_long_words_gen(items):
    max_len = 0
    cache = []
    for item in items:
        size = len(item)
        if size > max_len:
            # this item is now the longest item
            max_len = size
            # anything previously seen can be yielded and purged from the cache
            yield from (cached for _, cached in cache)
            cache = [(size, item)]
        else:
            # this item might be smaller than the max item,
            # but cannot be yielded yet to preserve the collection's order
            cache.append((size, item))

    # and now cleanup
    for size, item in cache:
        if size != max_len:
            yield item


def remove_long_words(items):
    return list(remove_long_words_gen(items))


words_list = ['fish', 'barrel', 'like', 'shooting', 'sand', 'bank']
actual = remove_long_words(words_list)
expected = ['fish', 'barrel', 'like', 'sand', 'bank']
assert actual == expected
It is slower, though, at least for such a small sample size:
def remove_long_words_nilamo(items):
    return list(remove_long_words_gen(items))


def remove_long_words_perfringo(list_):
    max_lenght = max(len(word) for word in list_)
    return [word for word in list_ if len(word) < max_lenght]


words_list = ['fish', 'barrel', 'like', 'shooting', 'sand', 'bank']
print("single-pass:",
      timeit.timeit("remove_long_words_nilamo(words_list)", globals=globals()))
print("two-pass:", timeit.timeit("remove_long_words_perfringo(words_list)", globals=globals()))
Output:
single-pass: 3.5252245 two-pass: 2.3057698
Reply


Messages In This Thread
RE: Removing all strings in a list that are of x length - by nilamo - May-07-2021, 06:59 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Removing existing tuples from a list of tuple Bruizeh 4 2,810 May-15-2021, 07:14 PM
Last Post: deanhystad
Question Recursion and permutations: print all permutations filling a list of length N SantiagoPB 6 3,353 Apr-09-2021, 12:36 PM
Last Post: GOTO10
  Removing items in a list cap510 3 2,362 Nov-01-2020, 09:53 PM
Last Post: cap510
  How do you find the length of an element of a list? pav1983 13 4,939 Jun-13-2020, 12:06 AM
Last Post: pav1983
  list of strings to list of float undoredo 3 2,713 Feb-19-2020, 08:51 AM
Last Post: undoredo
  Removing items from list slackerman73 8 4,468 Dec-13-2019, 05:39 PM
Last Post: Clunk_Head
  Convert a list of integers to strings? Cornelis 3 2,291 Nov-15-2019, 12:13 PM
Last Post: perfringo
  Print The Length Of The Longest Run in a User Input List Ashman111 3 3,231 Oct-26-2018, 06:56 PM
Last Post: gruntfutuk
  List of Strings Problem jge047 3 3,649 Dec-19-2017, 04:06 AM
Last Post: dwiga
  need help removing an item from a list jhenry 4 4,215 Oct-13-2017, 08:15 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020