list = [
{'name': 'Pavel', 'id': '233082', 'pos': ['85742', '95380', '40979']},
...
{'name': 'Oleg', 'id': '233082', 'pos': ['81052', '8316', '3062']},
{'name': 'Oleg', 'id': '233082', 'pos': ['41052', '5318', '2304']},
{'name': 'Oleg', 'id': '233082', 'pos': ['21050', '1317', '9305.']},
...
{'name': 'Nikolay', 'id': '233082', 'pos': ['25781', '94116', '20264']},
]
list = [
{'name': 'Pavel', 'id': '233082', 'pos': ['85742', '95380', '40979']},
...
{'name': 'Oleg', 'id': '233082', 'pos': [['81052', '8316', '3062'],
['41052', '5318', '2304'],
['21050', '1317', '9305.']]
},
...
{'name': 'Nikolay', 'id': '233082', 'pos': ['25781', '94116', '20264']},
]
from itertools import groupby
[
dict(
name=name,
id=id,
pos=[subitem['pos'] for subitem in subitems]
)
for (name, id), subitems in
groupby(
dic,
lambda item: (item['name'], item['id'])
)
]
from pprint import pprint
import pandas as pd
dic = [
{'name': 'Pavel', 'id': '233082', 'pos': ['85742', '95380', '40979']},
{'name': 'Oleg', 'id': '233082', 'pos': ['81052', '8316', '3062']},
{'name': 'Oleg', 'id': '233082', 'pos': ['41052', '5318', '2304']},
{'name': 'Oleg', 'id': '233082', 'pos': ['21050', '1317', '9305.']},
{'name': 'Nikolay', 'id': '233082', 'pos': ['25781', '94116', '20264']},
]
df = pd.DataFrame(dic)
# print(df)
# name id pos
# 0 Pavel 233082 [85742, 95380, 40979]
# 1 Oleg 233082 [81052, 8316, 3062]
# 2 Oleg 233082 [41052, 5318, 2304]
# 3 Oleg 233082 [21050, 1317, 9305.]
# 4 Nikolay 233082 [25781, 94116, 20264]
foo = {'pos': lambda rows: [x for x in rows] if len(rows) > 1 else rows, 'name': 'first', 'id': 'first'}
df = df.groupby('name').aggregate(foo).to_dict(orient='record')
# pprint(df)
# [{'id': '233082', 'name': 'Nikolay', 'pos': ['25781', '94116', '20264']},
# {'id': '233082',
# 'name': 'Oleg',
# 'pos': [['81052', '8316', '3062'],
# ['41052', '5318', '2304'],
# ['21050', '1317', '9305.']]},
# {'id': '233082', 'name': 'Pavel', 'pos': ['85742', '95380', '40979']}]