Do you want a list of dictionaries:
def office():
return dict(mylist=[{o.id:o.name} for o in db(db.table).select()])
Or do you want a single dictionary, with each id as a key:
def office():
return dict([(o.id, o.name) for o in db(db.table).select()])
Anthony
On Tuesday, December 11, 2012 8:11:17 AM UTC-5, software.ted wrote:
>
> I am getting the following error still after removing the extra (
>
> def office():
> return {o.id:o.name for o in db(db.table).select()}
>
> Traceback (most recent call last):
> File "/home/www-data/web2py/gluon/contrib/shell.py", line 188, in run
> compiled = compile(statement, '<string>', 'single')
> File "<string>", line 2
> return {o.id:o.name for o in db(db.table).select()}
>
>
> on the other method I am getting the following error
>
> In [5] : def offices():
> return dict((o.id,o.name) for o in db(db.table).select())
>
>
> In [6] : print offices()
> Traceback (most recent call last):
> File "/home/www-data/web2py/gluon/contrib/shell.py", line 235, in run
> exec compiled in statement_module.__dict__
> File "<string>", line 1, in <module>
> TypeError: offices() takes exactly 1 argument (0 given)
>
> Kind regards,
>
>
> On Tue, Dec 11, 2012 at 2:03 PM, simon <[email protected] <javascript:>>wrote:
>
>> You seem to have an extra (
>>
>>
>> On Tuesday, 11 December 2012 11:59:23 UTC, software.ted wrote:
>>>
>>> What am i missing with these statement:
>>>
>>> def getDict():
>>> return {(o.id:o.name for o in db(db.table).select()}
>>>
>>> or
>>>
>>> def getDict():
>>> return dict((o.id:o.name for o in db(db.table).select())
>>>
>>> getting same syntax error for both, whats the correct way of returning a
>>> dictionary?
>>>
>>> Teddy L.
>>>
>>>
>>> --
>>
>>
>>
>>
>
>
>
> --
>
> .......................................................................................
> Teddy Lubasi Nyambe
> Opensource Zambia
> Lusaka, ZAMBIA
>
> Cell: +260 97 7760473
> website: http://www.opensource.org.zm
>
> ~/
> Human Knowledge belongs to the world! - AntiTrust
>
> Man is a tool-using animal. Without tools he is nothing, with tools he is
> all - Thomas Carlyle 1795-1881
>
> /~
>
--