-3

How could I integrate multiple values as parameters without using a tuple or a list? Does python have overloaded parameters?

I'm thinking something like this:

add(1,2)      # 3
add(1,2,3,4)  # 10

def add(numbers):
    return sum(list(numbers))

Thanks in advance.

Charles Clayton
  • 17,005
  • 11
  • 87
  • 120
  • 2
    Have a look at [`*args` (argument lists)](https://docs.python.org/2/tutorial/controlflow.html#arbitrary-argument-lists) – hlt Aug 18 '14 at 23:28
  • 1
    The questions you've stated have nothing to do with your title. Please rewrite your title to address the question you are actually asking. – skrrgwasme Aug 18 '14 at 23:41

1 Answers1

3

You are probably looking for Arbitrary Argument List.

However, you are probably doing more work than you need to and also I am not entirely sure what you are exactly trying to do (or what you mean by efficient algorithm), but the sum builtin function already does what you needed, you just need to read the documentation on what it actually expects in its arguments.

>>> sum([1,2,3])
6
>>> sum([1,2,3,4,5,6])
21

Or heck, if you want to go without a list or tuple (actually, arguments are a list already so you are just using unnecessary syntactic sugar) you can just do this then.

def add(*numbers):
    return sum(numbers)

Just think a little and you can put together the above function.

metatoaster
  • 17,419
  • 5
  • 55
  • 66