Python create nested dictionary from list -
Python create nested dictionary from list -
i query db list of products want transform dict. that's model:
class product(db.model): __tablename__ = 'products' id = db.column(db.unicode(64), primary_key=true) name = db.column(db.unicode(2048), nullable=false) descripton = db.column(db.unicode(2048), nullable=false)
that's want result:
result = { id: { name: name, description: description}, ... }
can utilize list comprehension syntax this?
result = {p.id: {name: p.name, description: p.description p in products.all()}
of course of study can. take this:
{p.id: {name: p.name, description: p.description} p in products.all()}
but sure need it? can override __getitem__
method:
class product(db.model): __tablename__ = 'products' id = db.column(db.unicode(64), primary_key=true) name = db.column(db.unicode(2048), nullable=false) descripton = db.column(db.unicode(2048), nullable=false) def __getitem__(self, key): # code find object in db , transform them if want.
python
Comments
Post a Comment