0

I have this list of dictionaries:

cust = [
        {"id": 1, "name": u"name 1", "bill_amount": 1000},
        {"id": 2, "name": u"name 2", "bill_amount": 5000},
        {"id": 3, "name": u"name 3", "bill_amount": 7600},
        {"id": 4, "name": u"name 4", "bill_amount": 30}
       ]

And I want to get a list of just the names.

Trying this:

def getName(x): x["name"]
print map(getName, cust)

Returns this:

[None, None, None, None]

Why? Am I missing something obvious?

ninjagecko
  • 88,546
  • 24
  • 137
  • 145
empz
  • 11,509
  • 16
  • 65
  • 106

3 Answers3

6

You could also use operator.itemgetter() instead of defining your own function for this:

>>> import operator
>>> map(operator.itemgetter("name"), cust)
[u'name 1', u'name 2', u'name 3', u'name 4']
Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
3
def getName(x):
    return x["name"]

In python, a function which returns nothing returns None. Do not confuse this with lambda-syntax (useful but not "pythonic"), which would have been: getName = lambda x: x["name"]

ninjagecko
  • 88,546
  • 24
  • 137
  • 145
  • Oh ok, I thought it was like ruby that the last expression is returned. Thanks – empz Mar 15 '12 at 00:49
  • 2
    Btw, why lambdas are not considered "pythonic" ? – empz Mar 15 '12 at 00:50
  • according to Guido: "I've never liked lambda – crippled (only one expression) – confusing (no argument list parentheses) – can use a local function instead" – wim Mar 15 '12 at 00:54
  • 1
    @emzero: "pythonic" means "the normal way to do things in python". The main author of python doesn't like what code which abuses lambdas, so he made it hard to code with them. You cannot have multi-line lambdas, and there almost always a cleaner way to do things in python without using lambdas. Usually you'd just do `def myFunction(...):` and as many indented lines of code as you want. You can do this anywhere, even inside other functions. The only downside is it is sometimes a bit verbose, and forces you to give a name to a function you don't need to name. – ninjagecko Mar 15 '12 at 00:56
  • @emzero: Probably because they are redundant, limited and the same can be done using cleaner syntax of function definition. Even Guido Van Rossum (the creator of Python) has stated that he would rather not create lambdas if he could decide about that again, as far as I know. Being able to hold only one expression in lambda is really limiting and makes the code unpythonic in complex cases. – Tadeck Mar 15 '12 at 00:58
  • I don't think he "made it hard to code with them". He made it easy to code without them! – wim Mar 15 '12 at 00:59
  • @Tadack: This is misleading. *Python's* lambdas are crippled by design with (as you put it) "limitation" and "unclean syntax". Lambdas in other programming languages (like LISP and Haskell) are the basic building block and incredibly versatile, and can certainly "hold more than one expression". Whether python would be a better language without lambdas, though, I do not necessarily disagree with; though it's a discussion I'd rather avoid. – ninjagecko Mar 15 '12 at 01:00
  • @wim: Indeed, I don't disagree =) I was even hinting at that, with "almost always a cleaner way to do things in python without using lambdas". – ninjagecko Mar 15 '12 at 01:02
  • 2
    For the record, there are some of us who like lambdas -- even Python's lambdas -- a lot and think they're about the right size, and will almost always write a lambda rather than importing itemgetter from operator. While this is probably the minority view, there are enough who feel this way that the pushback against removing them ensured that lambdas survive to this day.. (which makes me happy, not least because I was on the losing side of the @ vote.) – DSM Mar 15 '12 at 01:05
  • @ninjagecko: I was talking about _lambdas_ (or _anonymous functions_) in Python. I wasn't talking about _lambdas_ in general. Of course there are implementations of lambdas that could be very useful (other examples of complex anonymous functions in popular languages include JavaScript, PHP > 5.3, Ruby etc.). – Tadeck Mar 15 '12 at 01:10
3

As already pointed out, your function is not returning anything.

For the record, the pythonic way to do this is not to use map, but to use a list comprehension.

[d['name'] for d in cust]
wim
  • 338,267
  • 99
  • 616
  • 750
  • I believe this is arguable (the usage of `map()` vs. list comprehensions) and it depends whether the function you want to apply is already defined or not. Anyway +1 for another option. – Tadeck Mar 15 '12 at 01:02
  • I don't have any problem with `map()` personally, but Guido says "list comprehensions do the same thing better". source: slide 4 of [python regrets](http://www.python.org/doc/essays/ppt/regrets/PythonRegrets.pdf) – wim Mar 15 '12 at 01:06
  • I believe some things have changed since 2002 and even if `reduce()` has been removed from builtins in 3.x, `map()` stays and [is sometimes even more efficient](http://stackoverflow.com/a/1247490/548696) (and cleaner, but this is rather subjective). – Tadeck Mar 15 '12 at 01:19