In Python, you can create a list of dictionaries using the `list` class for the outer list and `dict` class for the inner dictionaries.
In this example,
dict1
and dict2
using the dict
class.list_of_dicts
using the list
class and initialize it with the inner dictionaries.dict1 = {'key1': 'value1', 'key2': 'value2'}
dict2 = {'key3': 'value3', 'key4': 'value4'}
list_of_dicts = [dict1, dict2]
print(list_of_dicts)
[{'key1': 'value1', 'key2': 'value2'}, {'key3': 'value3', 'key4': 'value4'}]
In this example,
list_of_dicts_empty
using the list
class.dict3
and dict4
using the dict
class.append
method to add the inner dictionaries dict3
and dict4
to the outer list list_of_dicts_empty
.list_of_dicts_empty = []
dict3 = {}
dict4 = {}
list_of_dicts_empty.append(dict3)
list_of_dicts_empty.append(dict4)
print(list_of_dicts_empty)
[{}, {}]
In this tutorial, we learned How to create a List of Dictionaries in Python language with well detailed examples.