469,592 Members | 1,713 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,592 developers. It's quick & easy.

creating and appending to a dictionary of a list of lists

Hey,

I started with this:

factByClass = {}

def update(key, x0, x1, x2, x3):
x = factByClass.setdefault(key, [ [], [], [], [] ])
x[0].append(x0)
x[1].append(x1)
x[2].append(x2)
x[3].append(x3)

update('one', 1, 2, 3, 4)
update('one', 5, 6, 7, 8)
update('two', 9, 10, 11, 12)

print factByClass

{'two': [[9], [10], [11], [12]], 'one': [[1, 5], [2, 6], [3, 7], [4,
8]]}

I then 'upgraded' to this:

def update(key, *args):
x = factByClass.setdefault(key, [[], [], [], [] ])
for i, v in enumerate(args):
x[i].append(v)

Is there a better way?
Cheers!

Aug 15 '07 #1
2 3518
Ant
On Aug 15, 3:30 am, pyscottish...@hotmail.com wrote:
Hey,

I started with this:

factByClass = {}
....
def update(key, *args):
x = factByClass.setdefault(key, [[], [], [], [] ])
for i, v in enumerate(args):
x[i].append(v)

Is there a better way?
Well, the following is perhaps neater:
>>factByClass = defaultdict(lambda: [[],[],[],[]])
def update(key, *args):
.... map(list.append, factByClass[key], args)
....
>>update('one', 1, 2, 3, 4)
update('one', 5, 6, 7, 8)
update('two', 9, 10, 11, 12)

print factByClass
defaultdict(<function <lambdaat 0x00F73430>, {'two': [[9], [1
0], [11], [12]], 'one': [[1, 5], [2, 6], [3, 7], [4, 8]]})

It abuses the fact that list.append modifies the list in place -
normally you would use map to get a new list object. In this case the
new list returned by map is just a list of None's (since append
returns None - a common idiom for functions that operate by side
effect), and so is not used directly.

--
Ant...

http://antroy.blogspot.com/

Aug 15 '07 #2
On Aug 15, 8:08 am, Ant <ant...@gmail.comwrote:
On Aug 15, 3:30 am, pyscottish...@hotmail.com wrote:
Hey,
I started with this:
factByClass = {}

...
def update(key, *args):
x = factByClass.setdefault(key, [[], [], [], [] ])
for i, v in enumerate(args):
x[i].append(v)
Is there a better way?

Well, the following is perhaps neater:
>factByClass = defaultdict(lambda: [[],[],[],[]])
def update(key, *args):

... map(list.append, factByClass[key], args)
...>>update('one', 1, 2, 3, 4)
>update('one', 5, 6, 7, 8)
update('two', 9, 10, 11, 12)
>print factByClass

defaultdict(<function <lambdaat 0x00F73430>, {'two': [[9], [1
0], [11], [12]], 'one': [[1, 5], [2, 6], [3, 7], [4, 8]]})

It abuses the fact that list.append modifies the list in place -
normally you would use map to get a new list object. In this case the
new list returned by map is just a list of None's (since append
returns None - a common idiom for functions that operate by side
effect), and so is not used directly.

--
Ant...

http://antroy.blogspot.com/
Nice. I like it. Thanks a lot!

Aug 15 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by John Mudd | last post: by
23 posts views Thread by Fuzzyman | last post: by
4 posts views Thread by bucket79 | last post: by
10 posts views Thread by Ben | last post: by
1 post views Thread by Eran | last post: by
19 posts views Thread by Dr Mephesto | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.