1

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

2
  • 1
    Possible duplicate of Using an index to get an item, Python Commented Apr 23, 2018 at 22:51
  • I'm unclear what you want the result to be. Do you want a dictionary of dictionaries? Python doesn't really allow for dynamically assigning a different number of variables. Commented Apr 23, 2018 at 23:14

2 Answers 2

1

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] 
Sign up to request clarification or add additional context in comments.

Comments

0

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'}]

2 Comments

what if the 2 dictionaries became 3 , how to make it dynamic ?
in python3 you can unpack like a, *b = [1, 2, 3] where everything else goes into b, but otherwise I would say use a for loop and perform whatever operation you need to on it in a throwaway variable for d in dict_list:

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.