Why does one work, but not the other?

J

j_mckitrick

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
 
A

Adonis

----- Original Message -----
From: "j_mckitrick" <[email protected]>
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:

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

Hope this helps.

Adonis
 
J

j_mckitrick

Thanks for the help, guys. I found the problem. The liststore cannot
be redefined once set up for the TreeView. I can only clear/append to
it.

But I'm still on my mission to replace 'for' with list comprehensions
where possible, according to the article on optimization on the python
site.


That being said, is there a way to write this as a comprehension? I
can't figure out how to do so and get k into the key correctly. I'm
just trying to save a dictionary via anydbm.

for k, v in self.options.items():
db[k] = str(v)

jonathon
 
P

Peter Otten

j_mckitrick said:
But I'm still on my mission to replace 'for' with list comprehensions
where possible, according to the article on optimization on the python
site.

I don't know the article, but I assume it doesn't tell list comprehensions
are always faster/better.
That being said, is there a way to write this as a comprehension? I
can't figure out how to do so and get k into the key correctly. I'm
just trying to save a dictionary via anydbm.

for k, v in self.options.items():
db[k] = str(v)
Yes,
dk = {1:2, 3:4}
options = {1:4, 2:6, 3:8}
dk.update(dict([(k, str(v)) for (k, v) in options.iteritems()]))
dk {1: '4', 2: '6', 3: '8'}

but why would you trade a muddy comprehension for a clean loop? The for loop
is clearer (and faster, I suppose) here. Remember that list comprehensions
are a means rather than an end.

With 2.4 that may be a different story, as the above will reduce (I think)
to

dk.update((k, str(v)) for (k, v) in options.iteritems())

However, some overhead (generating throwaway tuples) is likely to remain.

Peter
 
J

j_mckitrick

Peter Otten said:
I don't know the article, but I assume it doesn't tell list comprehensions
are always faster/better.

from http://www.python.org/doc/essays/list2str.html:

Try to use map(), filter() or reduce() to replace an explicit for
loop, but only if you can use a built-in function: map with a built-in
function beats for loop, but a for loop with in-line code beats map
with a lambda function!


I remember another, but can't find it right now.
 
T

Terry Reedy

j_mckitrick said:
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??

For questions like this, about supposedly anomalous behavior, you usually
need to give actual input and output, reduced to the minimum code needed to
show the purported behavior. Otherwise, the easiest guess is that you did
not use the same value of data in the two snippets;-)

Terry J. Reedy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top