libb.hashby

hashby(iterable, keyfunc)[source]

Create a dictionary from iterable using a key function.

Parameters:
  • iterable – Items to hash.

  • keyfunc – Function to extract key from each item.

Returns:

Dictionary mapping keys to items.

Return type:

dict

Example:

>>> items = [{'id': 1, 'name': 'a'}, {'id': 2, 'name': 'b'}]
>>> hashby(items, lambda x: x['id'])
{1: {'id': 1, 'name': 'a'}, 2: {'id': 2, 'name': 'b'}}