How to go through a list without using a "for i" loop

647 views python
6

When the function gets called, the *args are a tuple so I have to append each argument to a list first. Then I sum them by mapping their values to float and using the sum() function.

There is nothing wrong whith this block of code, but I get a warning saying "Unused variable i".

def function(*args):
    x = []
    y = 0
    for i in range(len(args)):
        x.append(args[y])
        y += 1
   x = list(map(float, x))
   return sum(x)

Is there a way to do this in some other way that doesn't envolve using a "for i" loop and is more efficient?

answered question

1 Answer

13

for _ in range(len(args)):

That will ignore using a variable

posted this

Have an answer?

JD

Please login first before posting an answer.