Ad

Creating A List Of Existing Dictionaries In Python

- 1 answer

I need to get a list of some dictionaries that I defined before.The dictionaries look like these: dic1={"D":1.4,"SD":1.4} dic2={"D":1.2,"SD":1.2,"L":1.6,"LR":1.6,"LR0.5":1.6,"LP":1.6,"LRF":0.5} dic3={"D":1.2,"SD":1.2,"L":1,"LR":1,"LR0.5":1,"LP":1,"LRF":1.6} And I expect a list like this:

list=[{"D":1.4,"SD":1.4},{"D":1.2,"SD":1.2,"L":1.6,"LR":1.6,"LR0.5":1.6,"LP":1.6,"LRF":0.5},                                                                               {"D":1.2,"SD":1.2,"L":1,"LR":1,"LR0.5":1,"LP":1,"LRF":1.6}]

How can I create it using a loop in python?

Ad

Answer

list(filter(lambda a: type(a) == dict, locals().values()))

Will return all local variables with dict type as a list

Ad
source: stackoverflow.com
Ad