Moo
Moo

Reputation: 27

splitting nested dictionaries in python

I have a simple thing but for some reason my mind is blocked now what is the name of this type of data

[{'self': 'test', 'id': 4, 'name': 'IT Network Support'}, {'self': 'tes_1', 'id': 5, 'name': 'IT PC Support'}]

how to split them into 2 dictionaries

it can be also dynamic and change to

[{'self': 'test', 'id': 4, 'name': 'IT Network Support'}, {'self': 'tes_1', 'id': 5, 'name': 'IT PC Support'} ,{'self': 'tes_2', 'id': 6, 'name': 'IT Voice Support'} ]

I want a solution which can be dynamic

Upvotes: 1

Views: 971

Answers (2)

rsiemens
rsiemens

Reputation: 615

You can unpack iterables in python!

a, b = [{'self': 'test', 'id': 4, 'name': 'IT Network Support'}, {'self': 'tes_1', 'id': 5, 'name': 'IT PC Support'}]

Upvotes: 0

James
James

Reputation: 2711

Easiest way would be:

dict1, dict2 = [{'self': 'test', 'id': 4, 'name': 'IT Network Support'}, {'self': 'tes_1', 'id': 5, 'name': 'IT PC Support'}]

Which unpacks them into the variable dict1 and dict2

You can also just index into the list directly e.g.

dict_list = [{'self': 'test', 'id': 4, 'name': 'IT Network Support'}, {'self': 'tes_1', 'id': 5, 'name': 'IT PC Support'}]
dict1 = dict_list[0]
dict2 = dict_list[1]

Upvotes: 1

Related Questions