Can python create a dictionary from a list comprehension?

half.italian at gmail.com half.italian at gmail.com
Sun May 27 17:03:34 EDT 2007


On May 27, 1:55 pm, erikcw <erikwickst... at gmail.com> wrote:
> Hi,
>
> I'm trying to turn o list of objects into a dictionary using a list
> comprehension.
>
> Something like
>
> entries = {}
>  [entries[int(d.date.strftime('%m'))] = d.id] for d in links]
>
> I keep getting errors when I try to do it.  Is it possible?  Do
> dictionary objects have a method equivalent to [].append?  Maybe a
> lambda?
>
> Thanks for your help!
> Erik

try...

[entries.__setitem__(int(d.date.strftime('%m'))], d.id) for d in
links]

btw...I was curious of this too.  I used 'dir(dict)' and looked for a
method that might do what we wanted and bingo!

~Sean




More information about the Python-list mailing list