Is there a better way to do this snippet?

Discussion in 'Python' started by python, Apr 3, 2012.

  1. python

    python Guest

    I played around with a few things and this works but was wondering if
    there was a better way to do this.
    My first thought was list comprehension but could not get a figure out
    the syntax.

    tag23gr is a list of lists each with two items.
    g23tag is an empty dictionary when I run the for loop below.
    When is is complete each key is a graphic name who's values are a list
    of tags.

    for item in tag23gr:
    .... value, key = tuple(item)
    .... if(g23tag.get(key)):
    .... g23tag[key].append(value)
    .... else:
    .... g23tag[key] = [value]
     
    python, Apr 3, 2012
    #1
    1. Advertising

  2. python <> writes:

    > tag23gr is a list of lists each with two items.
    > g23tag is an empty dictionary when I run the for loop below.
    > When is is complete each key is a graphic name who's values are a list
    > of tags.
    >
    > for item in tag23gr:
    > ... value, key = tuple(item)
    > ... if(g23tag.get(key)):
    > ... g23tag[key].append(value)
    > ... else:
    > ... g23tag[key] = [value]


    for item in tag23gr:
    g23tag.setdefault(item[0],[]).append(item[1])

    -- Alain.
     
    Alain Ketterlin, Apr 3, 2012
    #2
    1. Advertising

  3. On Wed, Apr 4, 2012 at 12:36 AM, python <> wrote:
    > for item in tag23gr:
    > ...     value, key = tuple(item)
    > ...     if(g23tag.get(key)):
    > ...             g23tag[key].append(value)
    > ...     else:
    > ...             g23tag[key] = [value]


    Simple enhancement: Use setdefault. Instead of the if, just use:

    g23tag.setdefault(key,[]).append(value)

    That'll cover both cases in one.

    You can leave off the explicit tuple construction; if item is a
    two-element list, you can unpack it directly. You can also embed that
    straight into your for loop:

    for value,key in tag23gr:

    Do both and you cut your loop down to two lines. Cool! :)

    Chris Angelico
     
    Chris Angelico, Apr 3, 2012
    #3
  4. python

    Peter Otten Guest

    python wrote:

    > I played around with a few things and this works but was wondering if
    > there was a better way to do this.
    > My first thought was list comprehension but could not get a figure out
    > the syntax.
    >
    > tag23gr is a list of lists each with two items.
    > g23tag is an empty dictionary when I run the for loop below.
    > When is is complete each key is a graphic name who's values are a list
    > of tags.
    >
    > for item in tag23gr:
    > ... value, key = tuple(item)
    > ... if(g23tag.get(key)):


    That should be

    if key in g23tag:

    Your version means trouble for keys that evaluate to False in a boolean
    context, e. g. 0, False, None, "", (),...

    > ... g23tag[key].append(value)
    > ... else:
    > ... g23tag[key] = [value]


    from collections import defaultdict
    g23tag = defaultdict(list)

    for value, key in tag23gr:
    g23tag[key].append(value)
     
    Peter Otten, Apr 3, 2012
    #4
  5. python

    nn Guest

    On Apr 3, 11:02 am, Alain Ketterlin <-strasbg.fr>
    wrote:
    > python <> writes:
    > > tag23gr is a list of lists each with two items.
    > > g23tag is an empty dictionary when I run the for loop below.
    > > When is is complete each key is a graphic name who's values are a list
    > > of tags.

    >
    > > for item in tag23gr:
    > > ...        value, key = tuple(item)
    > > ...        if(g23tag.get(key)):
    > > ...                g23tag[key].append(value)
    > > ...        else:
    > > ...                g23tag[key] = [value]

    >
    > for item in tag23gr:
    >     g23tag.setdefault(item[0],[]).append(item[1])
    >
    > -- Alain.


    Or alternatively:

    from collections import defaultdict
    g23tag = defaultdict(list)
    for item in tag23gr:
    .....g23tag[item[0]].append(item[1])
     
    nn, Apr 3, 2012
    #5
  6. nn <> writes:

    >> > for item in tag23gr:
    >> > ...        value, key = tuple(item)
    >> > ...        if(g23tag.get(key)):
    >> > ...                g23tag[key].append(value)
    >> > ...        else:
    >> > ...                g23tag[key] = [value]

    >>
    >> for item in tag23gr:
    >>     g23tag.setdefault(item[0],[]).append(item[1])


    > Or alternatively:
    >
    > from collections import defaultdict
    > g23tag = defaultdict(list)
    > for item in tag23gr:
    > ....g23tag[item[0]].append(item[1])


    Very handy in that case, but in general I dislike the idea of silently
    inserting a default value when the access is a read, e.g., in
    x=g23tag[wrung]. Explicit is better than implicit, as they say. YMMV.

    -- Alain.
     
    Alain Ketterlin, Apr 3, 2012
    #6
  7. python

    nn Guest

    On Apr 3, 12:26 pm, Alain Ketterlin <-strasbg.fr>
    wrote:
    > nn <> writes:
    > >> > for item in tag23gr:
    > >> > ...        value, key = tuple(item)
    > >> > ...        if(g23tag.get(key)):
    > >> > ...                g23tag[key].append(value)
    > >> > ...        else:
    > >> > ...                g23tag[key] = [value]

    >
    > >> for item in tag23gr:
    > >>     g23tag.setdefault(item[0],[]).append(item[1])

    > > Or alternatively:

    >
    > > from collections import defaultdict
    > > g23tag = defaultdict(list)
    > > for item in tag23gr:
    > > ....g23tag[item[0]].append(item[1])

    >
    > Very handy in that case, but in general I dislike the idea of silently
    > inserting a default value when the access is a read, e.g., in
    > x=g23tag[wrung]. Explicit is better than implicit, as they say. YMMV.
    >
    > -- Alain.


    Valid point. Preferred choice depends on the access patterns to the
    dict (e.g. one write and multiple reads, multiple writes and one loop
    over items, etc.)
     
    nn, Apr 3, 2012
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Bencsik
    Replies:
    2
    Views:
    839
  2. Paul Rubin
    Replies:
    5
    Views:
    421
    Hendrik van Rooyen
    Aug 6, 2009
  3. Diwa
    Replies:
    0
    Views:
    994
  4. Krekna Mektek
    Replies:
    3
    Views:
    100
  5. Replies:
    2
    Views:
    56
    Mark H Harris
    May 13, 2014
Loading...

Share This Page