Ryanless
Ryanless

Reputation: 144

function parameter taken from a list

So i have a function which takes multiple values:

def kgV(a,b, *rest):
#the function itself doesnt matter for the question
#beside that it returns an int no matter how many arguments where passed

now i have a list or range():

# for example
myRange = range(2, 10)
myList = [2,9,15]

Now i want it to possible to give my Function the list or range as parameter so that it works as if i had given the every int of the range or list as parameter.

#how can i make this work?

kgV(myRange)
kgV(myList)

i tried some things like: for i in a etc but they all returned errors :-/

edit: i just solved it using a help function but it seems a very un pythonic way of doing it, so is there a more pythonic / generic way of doing it?

def kgVList(liste):
    result = liste[0]
    for i in liste:
        result = kgV(i, result)
    return result

Upvotes: 1

Views: 121

Answers (1)

Matthias Schreiber
Matthias Schreiber

Reputation: 2527

You can simply unpack the values like this:

kgV(*a)

Upvotes: 1

Related Questions