1

I would like to be able to take a list of unknown length in for function parameters like this:

With a list lis = [1, 2, 3, ...] and a function def func(a, b, c, ...): ... the elements of the list go into the function as individual parameters, func(1, 2, 3, ...) *not func([1, 2, 3, ...])

Is this possible?

2 Answers2

5

You can use * to connote many:

def func(*a):
    for value in a:
        print(value)

func(1,2,3)

func(1,2,3,4,5)

To input many items into this function from a list:

my_list = [i for i in range(20)]
func(*my_list)
Yaakov Bressler
  • 6,210
  • 2
  • 31
  • 51
1

The *args will give you all function parameters as a tuple:

def func(*a):
    a = list(a)
    print(a)

func(1,2,3)
func(1,2,3,4,5,6,7,8,9,10)

Or you can use it like this:

mylist = [i for i in range(10)]
func(*mylist)
ppwater
  • 2,304
  • 4
  • 14
  • 29