Search code examples
pythonlistfunctionreturnargs

python *args and lists


I have this assignment:

Define a function that takes in an arbitrary number of arguments, and returns a list containing only those arguments that are even. Don't run the function simply provide the definition.

What I've tried:

def myfunc(*args):
    return list(args%2==0)

Solution

  • It should be as follows:

    def a (*args):
      l = []
      for i in args:
        if i%2 == 0:
          l.append(i)
      return l