Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a list of values to a function

Tags:

python

Sorry for such a silly question, but sitting in front of the comp for many hours makes my head overheated, in other words — I'm totally confused. My task is to define a function that takes a list of words and returns something. How can I define a function that will take a list of words?

def function(list_of_words):
    do something

When running this script in Python IDLE we should suppose to write something like this:

>>> def function('this', 'is', 'a', 'list', 'of', 'words')

But Python errors that the function takes one argument, and six (arguments) are given. I guess I should give my list a variable name, i.e. list_of_words = ['this', 'is', 'a', 'list', 'of', 'words'], but ... how?

like image 522
Gusto Avatar asked Dec 30 '25 09:12

Gusto


1 Answers

Use code:

def function(*list_of_words):
     do something

list_of_words will be a tuple of arguments passed to a function.

like image 103
mwek Avatar answered Jan 02 '26 00:01

mwek