Rajeev Rajeev - 4 months ago 6
Python Question

Lambda expression with list operations

Shouldn't the below lambda expression return a list? Currently, it isn't.

>>> f=lambda l ,x:l.append(x)
>>> print f([1],3)

Ani Ani

append is a side-effecting function; it appends a value to an existing list without returning a value (or None, depending on how you want to think of it).

Perhaps you want:

lambda l, x: l + [x]

This creates a new list from the existing list with an element appended at the end.