Q:

Understanding the expression in Python

I an new to Python and do not understand the following expression 
tasks = [
            {
                'id': 1,
                'title': u'Buy groceries',
                'description': u'Milk, Cheese, Pizza, Fruit, Tylenol', 
                'done': False
            },
            {
                'id': 2,
                'title': u'Learn Python',
                'description': u'Need to find a good Python tutorial on the web', 
                'done': False
            }
         ]

and then
task = filter(lambda t: t['id'] == task_id, tasks)
if len(task) == 0:
    abort(404)
return jsonify( { 'task': task[0] } )

I do not fully understand the filter(lambda t:t['id']==task_id,tasks) portion of the code. Can anyone help me please ?

A:

lambda t:t['id']==task_id Is a function which returns a boolean. If t['id'] equals task_id, then the lambda will return True.
filter() goes through each element of tasks, assigning it to t. If the boolean is True, then it will remain in the returned list. If it is False, it is not included in the new list. I.E, it's filtered.
In other words, it's the same as [t for t in tasks if t['id'] == task_id]

Here's another example:
>>> mylist = range(10)
>>> filter(lambda x: x % 2 == 0, mylist)
[0, 2, 4, 6, 8]

This finds all the even numbers between 1 and 10.
It's also equivalent to:
>>> mylist = range(10)
>>> [x for x in mylist if x % 2 == 0]
[0, 2, 4, 6, 8]

