How to Create a Python Dictionary List?

Estimated read time 1 min read

In Python, you can create a list of dictionaries by using the {} syntax or the dict() constructor to create each dictionary, and then appending the dictionaries to a list using the append() method. Here’s an example:

my_list = []

dict1 = {'key1': 'value1', 'key2': 'value2'}
dict2 = dict(key1='value3', key2='value4')


print(my_list)  # Output: [{'key1': 'value1', 'key2': 'value2'}, {'key1': 'value3', 'key2': 'value4'}]

In this example, we create two dictionaries dict1 and dict2 using different syntaxes, and then append them to an empty list my_list using the append() method. We can then access the dictionaries in the list using list indexing, e.g. my_list[0] to access the first dictionary.

Note that the dictionaries in the list can have different keys and values, and you can add or modify dictionaries in the list using the standard dictionary methods, such as update(), pop(), etc.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply