Code Logo

Guest Shortlist with filter()

Published at22 Apr 2026
Python Functions Easy 0 views
Like0

An event coordinator is preparing a printed guest card area and wants to keep only names that are long enough to stand out clearly on the layout. The original guest list is already in order, and that order should not change in the final shortlist.

Your task is to return a new list containing only the names whose length is greater than or equal to a given minimum value. Names that are too short should be left out.

For example, if the guest list is ["Ayu", "Bima", "Cici", "Dea"] and the minimum length is 4, the result should be ["Bima", "Cici"]. The names "Ayu" and "Dea" are excluded because they have only 3 characters.

This challenge is about using a function to decide which values should stay and which should be removed. The final result should preserve the original left-to-right order of the names that pass the condition.

Example Input & Output

Example 1
Input
guests = ["Lina", "Rama"], min_length = 4
Output
["Lina", "Rama"]
Explanation

If every name meets the condition, all names are kept.

Example 2
Input
guests = ["Ayu", "Bima", "Cici", "Dea"], min_length = 4
Output
["Bima", "Cici"]
Explanation

Only names with at least 4 characters stay in the shortlist.

Example 3
Input
guests = [], min_length = 3
Output
[]
Explanation

An empty input list produces an empty shortlist.

Algorithm Flow

Recommendation Algorithm Flow for Guest Shortlist with filter()
Recommendation Algorithm Flow for Guest Shortlist with filter()

Solution Approach

This is a classic filtering task. Every guest name is checked against one rule, and only the names that pass that rule should remain in the output. Python's filter() is designed for exactly that pattern.

The key part of filter() is the function you give it. That function should return True when a name should stay and False when it should be removed. In this problem, the rule is based on len(name) >= min_length.

A direct version is:

return list(filter(lambda name: len(name) >= min_length, guests))

The lambda acts as the test function. Python applies it to each name in the list, keeps the ones that return True, and preserves their original order.

You can also define a small helper function inside the main function and pass that helper into filter(). That version is slightly longer, but it can be easier to read if you want the condition to have a clear name.

What makes this a functions problem is the way behavior is passed around. Instead of writing a long manual loop, you give filter() a function that explains the selection rule, and filter() handles the iteration for you.

The runtime is O(n) because each guest name is checked once, and the output list uses space for only the names that satisfy the condition.

Best Answers

python - Approach 1
def shortlist_guests(guests, min_length):
    def long_enough(name):
        return len(name) >= min_length
    return list(filter(long_enough, guests))