Rajeev Rajeev - 2 years ago 112
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
Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download