1

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
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218

1 Answers1

3

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.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218