Question: I have the lists of dictionary below:

```
data = [
{'name' : 'doan',
 'policies': [{'policy_start': '2019-05-01'},
          {'policy_start': '2019-05-02'}]},
{'name' : 'bui',
 'policies': [{'policy_start': '2019-05-03'},
              {'policy_start': '2019-05-04'}]},
{'name' : 'xuan',
 'policies': []}
]

```

how can `sorted` them by using first `policy_start` with result:

```
[{'name': 'bui', 'policies': [{'policy_start': '2019-05-03'}, {'policy_start': '2019-05-04'}]}, 
{'name': 'doan', 'policies': [{'policy_start': '2019-05-01'}, {'policy_start': '2019-05-02'}]},
{'name' : 'xuan','policies': []}]

```

I tried with function:

```
sorted(data, key=lambda item: item['policies'][0]['policy_start'], reverse=True)

```

but this just work with data:

```
data = [
{'name' : 'doan',
 'policies': [{'policy_start': '2019-05-01'},
              {'policy_start': '2019-05-02'}]},
{'name' : 'bui',
 'policies': [{'policy_start': '2019-05-03'},
              {'policy_start': '2019-05-04'}]}]

```

Answer: 
Try this,

`sorted(data, key=lambda item: item['policies'][0]['policy_start'] if item['policies'] else '', reverse=True)`
Try this:

```
from datetime import datetime
from datetime import  timedelta

some_old_date = (datetime.now() - timedelta(days=1000)).date()
key = lambda item: datetime.strptime(item['policies'][0]['policy_start'], '%Y-%m-%d').date()\
 if item['policies'] else some_old_date
sorted(data, key=key, reverse=True)

```

Output:

```
[{'name': 'bui',
  'policies': [{'policy_start': '2019-05-03'},
   {'policy_start': '2019-05-04'}]},
 {'name': 'doan',
  'policies': [{'policy_start': '2019-05-01'},
   {'policy_start': '2019-05-02'}]},
 {'name': 'xuan', 'policies': []}]

```