Python: How to merge or combain dynamic dictionaries from one varible

Viewed 31

I have a varible with some dictionaries inside it like this example:

dic = 
{'NO': '1', 'AGE': '45', 'STA': '0'}
{'NO': '2', 'AGE': '32', 'STA': '1'}
{'NO': '3', 'AGE': '18', 'STA': '2'}

YES, they are in one varible and without '[]' outside of them. When i use 'len' to get the lenght of the varible it shown me like that(3 is the lenght) :

dic = 
{'NO': '1', 'AGE': '45', 'STA': '0'}
3
{'NO': '2', 'AGE': '32', 'STA': '1'}
3
{'NO': '3', 'AGE': '18', 'STA': '2'}
3

So, i am confusing how to combain them into a ONE list(dict) like this:

dic = 
[{'NO': '1', 'AGE': '45', 'STA': '1'},{'NO': '2', 'AGE': '32', 'STA': '2'},{'NO': '3', 
'AGE': '18', 'STA': '3'}]

I know there are some ways to combain dictionaies, but my case is different because these dictionaies are in one varible and the number of dictionaies are not stable, i can not combain them one by one obviously. Here are some pic for this issue, It shows line by line when i print it, and the type is dict:

enter image description here

Anyone have idea for this? Thank you!

1 Answers

Well I tried playing around with the structure of dictionary you shared so the thing is in python your first dictionary will only be assigned to dic that means

dic={'NO': '1', 'AGE': '45', 'STA': '0'}

rest two dictionaries won't throw an error but since they are not assigned to a variable they will be lost. You can check yourself by doing print(dic,len(dic)) you will get the first dictionary and as its length is 3 so voila.

You can definitely take the 3 dictionaries into 3 different variable and then jo this.

l=[d1,d2,d3]

and you will get the desired result.

Related