Python - Split a List of Dicts into Individual Dicts

Python - split a list of dicts into individual dicts

You can unpack iterables with an assignment to a list of targets, like this

hub_score, auth_score = nx.hits(G)

Split a dictionary into a list of dictionaries

result = [{k: v} for (k, v) in mydict.iteritems()]

Split a list of dictionary of dictionaries into a single dictionary

You should not modify the object you are iterating over.

If you feed it to the pandas.json_normalize(...) you can see all that it is flattened.

>>> pd.json_normalize(b, sep="_").columns
Index(['id', 'idMemberCreator', 'type', 'date', 'appCreator', 'data_reason',
'data_board_id', 'data_organization_id', 'data_organization_name',
'memberCreator_id', 'memberCreator_username',
'memberCreator_activityBlocked', 'memberCreator_avatarHash',
'memberCreator_avatarUrl', 'memberCreator_fullName',
'memberCreator_idMemberReferrer', 'memberCreator_initials',
'memberCreator_nonPublicAvailable'],
dtype='object')

Also quick search pointed to https://www.geeksforgeeks.org/flattening-json-objects-in-python/

# Function for flattening 
# json
def flatten_json(y):
out = {}

def flatten(x, name =''):

# If the Nested key-value
# pair is of dict type
if type(x) is dict:

for a in x:
flatten(x[a], name + a + '_')

# If the Nested key-value
# pair is of list type
elif type(x) is list:

i = 0

for a in x:
flatten(a, name + str(i) + '_')
i += 1
else:
out[name[:-1]] = x

flatten(y)
return out

Which gives

>>> flatten_json(b)
{'id': 'xxxxx', 'idMemberCreator': 'xxxxx', 'data_reason': 'xxxxx', 'data_board_id': 'xxxxx', 'data_organization_id': 'xxxxx', 'data_organization_name': 'xxxxx', 'type': 'xxxxx', 'date': 'xxxxx', 'appCreator': 'xxxxx', 'memberCreator_id': 'xxxxx', 'memberCreator_username': 'xxxxx', 'memberCreator_activityBlocked': 'xxxxx', 'memberCreator_avatarHash': 'xxxxx', 'memberCreator_avatarUrl': 'xxxxx', 'memberCreator_fullName': 'xxxxx', 'memberCreator_idMemberReferrer': 'xxxxx', 'memberCreator_initials': 'x', 'memberCreator_nonPublicAvailable': 'xxxxx'}

python - split list of dictionaries into multiple lists of dictionaries, without grouping

You can create a nested dictionary with defaultdict, then call list() on its values:

>>> from collections import defaultdict
>>> interface_data = defaultdict(list)
>>> for i in s:
... key = i['NodeID'], i['Name']
... interface_data[key].append(i)

>>> list(interface_data.values())
[[{'NodeID': 1563,
'Name': 'GigabitEthernet1/1/1',
'InAveragebps': 0.03555526,
'OutAveragebps': 64.50593,
'DateTime': '2018-05-29T01:10:00.0000000'},
{'NodeID': 1563,
'Name': 'GigabitEthernet1/1/1',
'InAveragebps': 0.04555526,
'OutAveragebps': 6456.50593,
'DateTime': '2018-05-29T01:11:00.0000000'}],
[{'NodeID': 1788,
'Name': 'GigabitEthernet2/1/2',
'InAveragebps': 0.03554479,
'OutAveragebps': 64.7012558,
'DateTime': '2018-05-16T01:01:00.0000000'},
{'NodeID': 1788,
'Name': 'GigabitEthernet2/1/2',
'InAveragebps': 0.03555063,
'OutAveragebps': 64.62538,
'DateTime': '2018-05-17T01:011:00.0000000'}]]

It is perhaps a bit of a roundabout way to get back to a list result, but ultimately you want to do some type of membership testing (or, more like a lookup, in this case), and a dictionary is well-suited for that in the first place.

Before calling list(), interface_data is a nested dictionary; its keys are 2-tuples of (NodeID, Name) and its values are the dictionaries themselves.

>>> interface_data.keys()
dict_keys([(1563, 'GigabitEthernet1/1/1'), (1788, 'GigabitEthernet2/1/2')])

split list of dicts into chunk lists by returning specific value _ python

This will work. Let me know any questions.

my_list = [
{"name": "q", "id":1},
{"name": "w", "id":2},
{"name": "z", "id":3},
{"name": "f", "id":44},
{"name": "k", "id":55},
{"name": "d", "id":8},
{"name": "t", "id":25},
]

n = 4

x = [[y["id"] for y in my_list[i:i + n]] for i in range(0, len(my_list), n)]
print(x)


Related Topics



Leave a reply



Submit