FAQ
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

Search Discussions

  • Erik Max Francis at Jun 18, 2004 at 2:08 am

    j_mckitrick wrote:

    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??
    You're probably doing something else wrong; this fragment works fine:
    data = [1, 2, 3, 4]
    [[item] for item in data]
    [[1], [2], [3], [4]]

    --
    __ Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
    / \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
    \__/ Ferocious, aren't I?
    -- Lt. Vincent Hanna
  • Adonis at Jun 18, 2004 at 4:09 am
    ----- 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
  • J_mckitrick at Jun 18, 2004 at 10:26 am
    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
  • Peter Otten at Jun 18, 2004 at 11:36 am

    j_mckitrick wrote:

    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_mckitrick at Jun 18, 2004 at 6:34 pm
    Peter Otten <__peter__ at web.de> wrote in message news:<cauk4d$b4n$00$1 at news.t-online.com>...
    j_mckitrick wrote:
    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.
    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.
  • Robert Brewer at Jun 18, 2004 at 4:10 am

    j_mckitrick wrote:
    self.liststore = [[item] for item in data]
    I get an empty list! What gives??
    Dunno--I don't get an empty list.
    data = [1,2,3]
    [[item] for item in data]
    [[1], [2], [3]]

    Seems the problem lies elsewhere...


    Robert Brewer
    MIS
    Amor Ministries
    fumanchu at amor.org
  • Terry Reedy at Jun 18, 2004 at 6:59 pm
    "j_mckitrick" <j_mckitrick at bigfoot.com> wrote in message
    news:ec6dce8b.0406171754.5d5d84db at posting.google.com...
    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
  • Robert Brewer at Jun 18, 2004 at 8:59 pm

    j_mckitrick wrote:
    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)
    My usual solution to that is generically written:

    dict([(k, v) for k, v in d.iteritems()])

    ...taking advantage of the fact the you can pass dict() a list of
    tuples. Your specific example would be written:

    db = dict([(k, str(v)) for k, v in self.options.iteritems()])

    I use iteritems more often, since it doesn't create an intermediate
    structure.


    Robert Brewer
    MIS
    Amor Ministries
    fumanchu at amor.org

    P.S. Where is everyone on c.l.p. today? Brother Ray's memorial...?

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJun 18, '04 at 1:54a
activeJun 18, '04 at 8:59p
posts9
users6
websitepython.org

People

Translate

site design / logo © 2022 Grokbase