0

This may be a basic question, but I haven't been able to figure it out.

I have a function that takes in multiple arguments, as kwargs, so I can call it with 1, 2,3 or any amount of arguments. I also have a list whose length is unknown and I want to call the function with each item in the list.

So if I have a list like [1, 2, 3] I want to call the function like f(1,2,3) and if I have [1,2] f(1,2)

Frustrated
  • 111
  • 1
  • 6

1 Answers1

1

I think that should answere your question:

def func(*args):
    print(f'number of arguments: {len(args)}')
    print(f'first argument: {args[0]}')
    print(f'second argument: {args[1]}')
    print(f'last argument: {args[-1]}')

func(1, 2, 3)
func(1, 2)
func(*[1, 2, 3])
Tom
  • 54
  • 8