Why does one work, but not the other?

Adonis adonisv at REMTHISearthlink.net
Fri Jun 18 06:09:50 CEST 2004


----- Original Message ----- 
From: "j_mckitrick" <j_mckitrick at bigfoot.com>
Newsgroups: comp.lang.python
Sent: Thursday, June 17, 2004 9:54 PM
Subject: Why does one work, but not the other?


> I've done this before:
>
>         data = [self.cong.tm[k] for k in self.cong.tm.li]
> #li is list, tm is dict
>
> instead of:
>
>          for k in self.cong.tm.li:
>              data.append(self.cong.tm[k])
>
> but when I try:
>
>         self.liststore = [[item] for item in data]
>
>
> instead of:
>
>        for item in data:
>             self.liststore.append([item])
>
> I get an empty list!  What gives??
>
> jonathon

>>> tm = {'a':1, 'b':2, 'c':3}
>>> li = tm.keys()
>>> data = [tm[k] for k in li]
>>> data
[1, 3, 2]
>>> # also you can do
>>> data2 = [tm[k] for k in tm]
>>> data2
[1, 3, 2]

Check the dictionary, make sure it has entries?
When supplying the dictionary with elements given by the list, make sure the
keys exist.

or you can do:
>>> data = [tm.get(k) for k in li]

Will return None if a key does not exist to further debug.

Hope this helps.

Adonis





More information about the Python-list mailing list