On Apr 3, 11:02 am, Alain Ketterlin <al...@dpt-info.u-strasbg.fr>
wrote:
> python <w.g.sned...@gmail.com> 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])
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to