HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Filtering a list of names based on certain conditions

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
namesbasedconditionslistcertainfiltering

Problem

I wrote this code to filter a list of names based on certain conditions, and wanted to see if I can reduce this further and eliminate redundancy in code.

names1 = ["Jane", "Jake", "Bradley", "Bill", "Betty", "Kara", "Kris", "Jil"]
names2 = ["George", "Kate", "Karen", "Kurt", "Greg", "Gary"]
selection_criteria = ["full_list", "four_characters", "three_characters", "start_with_k", "start_with_z"]

def sublist (name_list, condition):
    return_list = []
    if condition == "full_list":
        return name_list
    if condition == "four_characters":
        for name in name_list:
            if len(name) == 4:
                return_list.append(name)
    if condition == "three_characters":
        for name in name_list:
            if len(name) == 3:
                return_list.append(name)
    if condition == "start_with_k":
        for name in name_list:
            if name[0] == 'K':
                return_list.append(name)
    if condition == "start_with_z":
        for name in name_list:
            if name[0] == 'Z':
                return_list.append(name)
    return return_list

for criteria in selection_criteria:
    print(sublist(names1, criteria))

for criteria in selection_criteria:
    print(sublist(names2, criteria))

Solution

I would make this two different functions, one for filtering all names of length n and one for all names starting with some letter. I made them into generator comprehensions, so they are more memory efficient on their own (even though here I cast them to lists, as soon as I want to use them).

Then there is a function which is similar to your function, except that it is configurable. By default it just yields the original list. If additional n's or c's are passed, it applies these criteria.

Finally there is a print function, which has your concrete realization of n = 4, 3 and c = "K", "Z".

def n_chars(names, n):
    return (name for name in names if len(name) == n)

def starts_with(names, c):
    return (name for name in names if name.lower().startswith(c))

def print_sublists(names):
    print(names)
    print(list(n_chars(names, 4))
    print(list(n_chars(names, 3))
    print(list(starts_with(names, "k"))
    print(list(starts_with(names, "z"))

if __name__ == '__main__':
    names1 = ["Jane", "Jake", "Bradley", "Bill", "Betty", "Kara", "Kris", "Jil"]
    names2 = ["George", "Kate", "Karen", "Kurt", "Greg", "Gary"] 
    for names in names1, names2:
        print_sublists(names)

Code Snippets

def n_chars(names, n):
    return (name for name in names if len(name) == n)

def starts_with(names, c):
    return (name for name in names if name.lower().startswith(c))

def print_sublists(names):
    print(names)
    print(list(n_chars(names, 4))
    print(list(n_chars(names, 3))
    print(list(starts_with(names, "k"))
    print(list(starts_with(names, "z"))

if __name__ == '__main__':
    names1 = ["Jane", "Jake", "Bradley", "Bill", "Betty", "Kara", "Kris", "Jil"]
    names2 = ["George", "Kate", "Karen", "Kurt", "Greg", "Gary"] 
    for names in names1, names2:
        print_sublists(names)

Context

StackExchange Code Review Q#148291, answer score: 5

Revisions (0)

No revisions yet.