Iterative multi-line single-line python (or two) using groupby

I want to turn a long data set into a wide one using functional and iterative tools, and I understand that this is a task for groupby. I asked a couple of questions about this before and thought I had it, but not quite in this case, which should be simpler:

Here are the data I have:

from itertools import groupby
from operator import itemgetter
from pprint import pprint

>>> longdat=[
{"id":"cat", "name" : "best meower", "value": 10},
{"id":"cat", "name" : "cleanest paws", "value": 8},
{"id":"cat", "name" : "fanciest", "value": 9},
{"id":"dog", "name" : "smelly", "value": 9},
{"id":"dog", "name" : "dumb", "value": 9},
]

Here is the format I want in:

>>> widedat=[
{"id":"cat", "best meower": 10, "cleanest paws": 8, "fanciest": 9},
{"id":"dog", "smelly": 9, "dumb": 9},
]

Here are my failed attempts:

# WRONG
>>> gh = groupby(sorted(longdat,key=id),itemgetter('id'))
>>> list(gh)
[('cat', <itertools._grouper object at 0x5d0b550>), ('dog', <itertools._grouper object at 0x5d0b210>)]

OK, you need to get the second element from the iterator, fairly fair.

#WRONG
>>> gh = groupby(sorted(longdat,key=id),itemgetter('id'))
>>> for g,v in gh:
...     {"id":i["id"], i["name"]:i["value"] for i in v}
                                      ^
SyntaxError: invalid syntax

Strange, it looked really. Let these loops relax to make sure.

#WRONG
gb = groupby(sorted(longdat,key=id),itemgetter('id'))
data = {}
for g,v in gb:
    data[g] = {}
    for i in v:
        data[g] = i

#WRONG
gb = groupby(sorted(longdat,key=id),itemgetter('id'))
data = []
for g,v in gb:
    for i in v:
        data[g] = i

Oh! OK, back to single line form

#WRONG
>>> gb = groupby(sorted(longdat,key=id),itemgetter('id'))
>>> [{"id":g, i["name"]:i["value"]} for i in k for g,k in gb]
[]

? ?! :

#WRONG
gb = groupby(sorted(longdat,key=id),itemgetter('id'))
for g,k in gb:
    for i in k:
       print(g, i["name"],i["value"])
cat best meower 10
cat fanciest 9
cat cleanest paws 8
dog smelly 9
dog dumb 9

, , - , , , .

, ?

, ,

>>> result[0]
{"id":"cat", "best meower": 10, "cleanest paws": 8, "fanciest": 9}

( , /all/where id == 'cat'?)

+4
1

key , sorted, id. .

itemgetter('id') lambda x: x.id.

>>> id(longdat[0])
41859624L
>>> id(longdat[1])
41860488L
>>> id(longdat[2])
41860200L
>>> itemgetter('id')(longdat[1])
'cat'
>>> itemgetter('id')(longdat[2])
'cat'
>>> itemgetter('id')(longdat[3])
'cat'

from itertools import groupby
from operator import itemgetter

longdat = [
    {"id":"cat", "name" : "best meower", "value": 10},
    {"id":"cat", "name" : "cleanest paws", "value": 8},
    {"id":"cat", "name" : "fanciest", "value": 9},
    {"id":"dog", "name" : "smelly", "value": 9},
    {"id":"dog", "name" : "dumb", "value": 9},
]

getid = itemgetter('id')
result = [
    dict([['id', key]] + [[d['name'], d['value']] for d in grp])
    for key, grp in groupby(sorted(longdat, key=getid), key=getid)
]
print(result)

:

[{'best meower': 10, 'fanciest': 9, 'id': 'cat', 'cleanest paws': 8},
 {'dumb': 9, 'smelly': 9, 'id': 'dog'}]
+4

All Articles