View on GitHub

Advance your python skills for data science

A collection of python tips and tricks that can be useful (for data scientists)

This page follows the examples from the official documentation on expressions in python

and

PEP 572

Some expressions

Assignement expressions

ORDERS = [{"id": "one"}, {"id": "two"}] 
order_id = "one"
if order := [order for order in ORDERS if order['id'] == order_id]:
    print(f"Order id {order_id} found!")

The above code is equivalent to checking the property of the list (len) to see if it is empty or not

order_id = 'one'
order = [order for order in ORDERS if order['id']==order_id]
if len(order) > 0:
    print(f"Order id {order_id} found!")