reduce python list of objects to dict object.id -> object
Posted
by Dmitry Teslenko
on Stack Overflow
See other posts from Stack Overflow
or by Dmitry Teslenko
Published on 2010-06-18T14:03:08Z
Indexed on
2010/06/18
14:13 UTC
Read the original article
Hit count: 242
Hello! You have list of objects each of them have id property. Here's my way to covert it to dict where keys are ids and values are objects:
reduce(
lambda x,y: dict(x.items() + { y.id : y}.items()),
list,
{}
)
Suggest better way to do it.
© Stack Overflow or respective owner