3

I am trying to write a function, that applies a function to a list. I am trying to capitalise all the words in a list but can't get it to work. Here is what I've done so far:

list = ("hello", "this", "is", "a", "test")

def firstFunction(x):
    return list.upper()

print firstFunction

The error I get is:

<function firstFunction at 0x0000000002352A58>

I'm really stuck on what to do next, any help would be greatly appreciated.

EDIT: I've just changed it but it's still not working:

mylist = ("hello", "this", "is", "james")

def firstFunction(x):
    return may(lambda: x.upper(), mylist)

print firstFunction()
JaAnTr
  • 896
  • 3
  • 16
  • 28
  • Try a list comprehension: [x.upper() for x in list] – paj28 Oct 25 '13 at 17:06
  • 1
    `list = ("hello", "this", "is", "a", "test")` is a tuple, an `immutable`, you cannot alter it !!! – Siva Cn Oct 25 '13 at 17:07
  • The question set states that I have to write a function and a list and apply the function to all elements of the list. What would be a better thing to do than capitalise it then? – JaAnTr Oct 25 '13 at 17:09
  • 1
    It's advisable not to name variables that are already reserved as python keywords such as `list,str,int,pass`,etc... – K DawG Oct 25 '13 at 17:12
  • `` is not an error. It is the string representation of the function object `firstFunction`. You are printing the function *object* with `print firstFunction`. You need to add parentheses to *call* the function. – SethMMorton Oct 25 '13 at 17:49

6 Answers6

4

That isn't an error. It is the function's address in memory. You are seeing it because you didn't invoke the function.

Overall, there are three problems with your code:

  1. You are not invoking the function. Adding (...) after it will do this.
  2. You are not passing in an argument to the function, which it requires.
  3. There is no upper method on a tuple (list in this case is a tuple).

Below is a fixed version of the code that does what I think you want:

# Don't name a variable 'list' -- it overshadows the built-in.
lst = ("hello", "this", "is", "a", "test")

def firstFunction(x):
    return tuple(y.upper() for y in x)

print firstFunction(lst)

Output:

('HELLO', 'THIS', 'IS', 'A', 'TEST')

Here are some references on everything done here:

http://docs.python.org/2/reference/compound_stmts.html#function-definitions

https://wiki.python.org/moin/Generators

http://docs.python.org/2.7/library/stdtypes.html#str.upper

  • This won’t work either as `firstFunction` takes one argument. The TO should a) remove the argument in the method definition or b) change `list.upper()` to `x.upper()` and pass `list` as the first argument. – Robin Krahl Oct 25 '13 at 17:05
  • Thanks it got rid of the error, but now I'm being told that firstFunction() takes exactly 1 argument (0 given). What does this mean? – JaAnTr Oct 25 '13 at 17:05
  • @JaAntr - That's because you need to give the function an argument. You built it to take one. See my edit. –  Oct 25 '13 at 17:13
  • add link to list comprehensions documentation? – Dave Oct 25 '13 at 17:26
  • @Dave - Well, I don't want to do that because I didn't use one here. What you see inside `tuple` is a _generator expression_, and I gave a reference to that. –  Oct 25 '13 at 17:29
4

While other answers are great, I want to mention that there's already a function in python, called map(), and it does almost exactly what you need:

Apply function to every item of iterable and return a list of the results..... The iterable arguments may be a sequence or any iterable object; the result is always a list.

So you code becomes

print map(str.upper, lst)

or, if you need a tuple, then:

print tuple(map(str.upper, lst))

You don't need anonymous lambda function here, because str.upper() accepts one argument. I think there's a debate about how pythonic this functional programming is, but I personally like it sometimes.

Roman Pekar
  • 107,110
  • 28
  • 195
  • 197
  • 1
    There is no upper global in Python. – Markus Unterwaditzer Oct 25 '13 at 17:19
  • Yea, you want `str.upper`. Also, the reason most people didn't use `map` is that a generator expression is often preferred. That is really just a style issue though. However, `map` _does_ return a list, so you need to put it in `tuple`. –  Oct 25 '13 at 17:21
  • @iCodez yeah, was on mobile, let me extend answer a bit – Roman Pekar Oct 25 '13 at 17:23
3

Actually nor the list, nor the tuple has no a method .upper(). So to achieve this you could just execute this statement:

print tuple(x.upper() for x in ("hello", "this", "is", "a", "test"))

http://codepad.org/MZ14yXeV

or this one:

print map(lambda x: x.upper(), ("hello", "this", "is", "a", "test"))

http://codepad.org/kc1LaNCY

Eugene Naydenov
  • 7,165
  • 2
  • 25
  • 43
1
list = ("hello", "this", "is", "a", "test")

is a tuple, an immutable, you cannot alter it, use,

print tuple((ele.upper() for ele in list))
Siva Cn
  • 929
  • 4
  • 10
1

I think this is the most pythonic one:

def cap(tup):
    return map(str.upper, tup)

>>> tup = ("hello", "this", "is", "a", "test")
>>> cap(tup)
['HELLO', 'THIS', 'IS', 'A', 'TEST']
>>>
octref
  • 6,521
  • 7
  • 28
  • 44
0

What you are trying to do involves list comprehensions:

print [firstFunction(x) for x in list]

what this does is: construct the list whose elements are the result of applying the function to each of the items in you input list and then printing it.

Some (hopefully helpful) comments

  • It is bad practice to name a variable list; even though it is not a keyword, it is the name of a fundamental python type, so re-binding it could cause confusion elsewhere.
  • In your definition def firstFunction(list) -- the name list that appears in the argument list does not have any relationship to the list variable that was defined earlier in your example. You may want to look at this question or the python documentation to understand how the scoping rules work in python.
Community
  • 1
  • 1
Dave
  • 7,555
  • 8
  • 46
  • 88