tags:

views:

141

answers:

4

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?

+9  A: 

Use code:

def function(*list_of_words):
     do something

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

katoda
maybe this one is correct too, but since I don't know what tuple is, I won't use it in my code
Gusto
Maybe learn what it is? A tuple is simply a collection of items, the container of which cannot be shrunk or expanded. Similar to a list, but without `.append()` or `.remove()`, if you will.
Santa
+2  A: 

It's simple:

list_of_words = ['this', 'is', 'a', 'list', 'of', 'words']
def function(list_of_words):
    do_something

That's all there is to it.

Mike Driscoll
@Mike, that's what I thought he was asking too, but it sounds like maybe he meant that each word should be passed as a separate argument to the function.
LarsH
The task is to take a (some/any) list of words, not a specific one.
Gusto
+1  A: 
>>> def function(list_of_words):
...     print( list_of_words )
... 
>>> 
>>> function('this', 'is', 'a', 'list', 'of', 'words')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: function() takes exactly 1 argument (6 given)
>>> function(['this', 'is', 'a', 'list', 'of', 'words'])
['this', 'is', 'a', 'list', 'of', 'words']

Works for me. What's going wrong for you? Can you be specific on what doesn't work for you?

S.Lott
you're right, I should put it in square brackets - this is the answer to mu question.P.S. I'm a beginner so don't judge)
Gusto
+5  A: 

Simply call your function with:

function( ['this', 'is', 'a', 'list', 'of', 'words'] )

This is passing a list as an argument.

Dan
THIS IS WHAT I NEED! THANKS A LOT, I WAS TOTALLY CONFUSED.
Gusto
@Gusto: [ur caps r on, btw](http://news.cnet.com/oprah-gets-pwned-by-shaq-on-twitter/)
Roger Pate