Is there a better way to do this snippet?

P

python

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]
 
A

Alain Ketterlin

python said:
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.
 
C

Chris Angelico

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
 
P

Peter Otten

python said:
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)
 
N

nn

python said:
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])
 
A

Alain Ketterlin

nn said:
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.
 
N

nn

nn said:
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.)
 

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,769
Messages
2,569,582
Members
45,064
Latest member
naturesElixirCBDReview

Latest Threads

Top