Sending each elements of a list as parameters of function

Viewed 119

How to send each list element as a function parameter in python?

list1 = ['38', '39', '40', ...]
any_function(list_item)

I can't know that length of the list that's why i can't send every element of the list as a function's parameter at the same time. Is there any Pythonic way?

2 Answers

Change your function parameter to e.g. *args:

list1 = ['38', '39', '40']

def printemall(*args):
    print(args)

printemall(list1)
# (['38', '39', '40'],)

printemall(*list1)
# ('38', '39', '40')
def any_function(item):
    print(item)

def calling_function():
    list1 = ['38','39', '40']
    for item in list1:
        any_function(item)
Related