[SOLVED] Pass list elements as arguments of a function one by one


Suppose I have a function to calculate the square of a certain number. Now, I have a list of, for example, 100 numbers that I need to pass as the arguments. How do I pass each element of the list, one by one, as the argument to get the results?


You can use a for loop:

def square(i):
    return i ** 2

lst = [1,2,3,4,...]
for i in lst:
    result = square(i)

This works because the iterator, i goes through every element in lst and passes it as a parameter into our function, square().

I hope this helped! Let me know if you need any further clarification or details 🙂

Answered By – Ani M

Answer Checked By – Gilberto Lyons (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *