Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Can python create a dictionary from a list comprehension?

Reply
Thread Tools

Can python create a dictionary from a list comprehension?

 
 
erikcw
Guest
Posts: n/a
 
      05-27-2007
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

 
Reply With Quote
 
 
 
 
half.italian@gmail.com
Guest
Posts: n/a
 
      05-27-2007
On May 27, 1:55 pm, erikcw <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Stefan Sonnenberg-Carstens
Guest
Posts: n/a
 
      05-27-2007
erikcw schrieb:
> 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
>
>

normally a dict(whatEver) will do

Example:

a = [1,2,3,4,5,6,7,8,9,10]

aDict = dict([(x,x+1) for x in a if x%2==0])

print aDict

 
Reply With Quote
 
Pierre Quentel
Guest
Posts: n/a
 
      05-27-2007
On 27 mai, 22:55, erikcw <(E-Mail Removed)> 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


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

With Python2.4 and above you can use a "generator expression"

entries = dict( (int(d.date.strftime('%m')),d.id) for d in links )


Regards,
Pierre

 
Reply With Quote
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      05-28-2007
In <(E-Mail Removed). com>, half.italian
wrote:

> [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!


This is really ugly. Except `__init__()` it's always a code smell if you
call a "magic" method directly instead of using the corresponding
syntactic sugar or built in function. And using a list comprehension just
for side effects is misleading because the reader expects a (useful) list
to be build when stumbling over a list comp and it's wasteful because an
unnecessary list of `None`\s is build and thrown away for no reason other
than to have a one liner. This is not Perl!

Ciao,
Marc 'BlackJack' Rintsch
 
Reply With Quote
 
Wim Vogelaar
Guest
Posts: n/a
 
      05-28-2007
> Example:
>
> a = [1,2,3,4,5,6,7,8,9,10]
>
> aDict = dict([(x,x+1) for x in a if x%2==0])
>
> print aDict
>


When I run this program I get:
{8: 9, 2: 3, 4: 5, 10: 11, 6: 7}

why this output isn't ordered, giving:
{2: 3, 4: 5, 6: 7, 8: 9, 10: 11 }



 
Reply With Quote
 
Maric Michaud
Guest
Posts: n/a
 
      05-28-2007
Pierre Quentel a écrit :
> On 27 mai, 22:55, erikcw <(E-Mail Removed)> wrote:
>> Hi,
>>
>> I'm trying to turn o list of objects into a dictionary using a list
>> comprehension.

....
>
> entries = dict([ (int(d.date.strftime('%m')),d.id) for d in links] )
>
> With Python2.4 and above you can use a "generator expression"
>
> entries = dict( (int(d.date.strftime('%m')),d.id) for d in links )
>


You can also create dictionaries knowing only the keys the same way (ie.
a two-dimensional array) :

In [77]: dict.fromkeys((a, b) for a in range(4) for b in range(2))
Out[78]:
{(0, 0): None,
(0, 1): None,
(1, 0): None,
(1, 1): None,
(2, 0): None,
(2, 1): None,
(3, 0): None,
(3, 1): None}
 
Reply With Quote
 
Tim Churches
Guest
Posts: n/a
 
      05-28-2007
http://shaheeilyas.com/flags/

Scroll to the bottom to see why this is not entirely off-topic. Are
there other public examples in which Python has been used to harvest and
represent public information in useful and/or interesting ways? Ideas
for some more?

Tim C


 
Reply With Quote
 
Wim Vogelaar
Guest
Posts: n/a
 
      05-28-2007

>
> why this output isn't ordered, giving:
> {2: 3, 4: 5, 6: 7, 8: 9, 10: 11 }
>
>


I made the original list two elements longer: a =
[1,2,3,4,5,6,7,8,9,10,11,12]

and to my surprise the output is now ordered, giving: {2: 3, 4: 5, 6: 7, 8:
9, 10: 11, 12: 13}

I am running ActiveState ActivePython 2.5




 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      05-28-2007
En Mon, 28 May 2007 05:20:16 -0300, Wim Vogelaar
<(E-Mail Removed)> escribió:

>> Example:
>>
>> a = [1,2,3,4,5,6,7,8,9,10]
>>
>> aDict = dict([(x,x+1) for x in a if x%2==0])
>>
>> print aDict
>>

>
> When I run this program I get:
> {8: 9, 2: 3, 4: 5, 10: 11, 6: 7}
>
> why this output isn't ordered, giving:
> {2: 3, 4: 5, 6: 7, 8: 9, 10: 11 }


A dictionary is not ordered, no matter how you create it. If you want to
process the keys in order:

for key in sorted(aDict):
print key, '=', aDict[key]

(Note that sorted(aDict) returns a *list*, not a dictionary!)

--
Gabriel Genellina

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Performance ordered dictionary vs normal dictionary Navkirat Singh Python 6 07-29-2010 10:18 AM
Re: Performance ordered dictionary vs normal dictionary Chris Rebert Python 0 07-29-2010 06:11 AM
creating a dictionary from a dictionary with regex james_027 Python 1 08-22-2007 07:39 AM
[DICTIONARY] - Copy dictionary entries to attributes Ilias Lazaridis Python 6 02-21-2006 11:27 AM
dictionary within dictionary Fox ASP General 5 03-13-2005 05:03 AM



Advertisments