'Not able to get all the names that are not beginning with 'a'

My own problem

Write a function Nameskip() that takes list of names and skips the names beginning with 'a'

def Nameskip(namelst):
    for name in namelst:
        if name[0] in 'a':
            namelst.pop()
        else:
            return names  #Not returning all names


Solution 1:[1]

You can use str.startswith in a list comprehension to filter out the names you want to keep

def Nameskip(namelst):
    return [name for name in namelst if not name.startswith('a')]

In the version you wrote, you should avoid mutating a list as you are iterating through it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cory Kramer