How to Create a List of Dictionary in Python

To create a list of dictionaries in Python, you can use [] square brackets along with {} curly braces or use the append() function.

The following examples show how to create a list of dictionaries in Python using two different methods.

Using [] Bracket & {} Braces

We can use [] square brackets and {} curly braces to create a list of dictionaries.

Suppose we have the following data:

# Declare list of dictionaries
Emp_data = [
    {'Emp_Id': 'A-12', 'Joining_Date': '11/12/2021', 'Emp_Name': 'Anita', 'Past_Exp': 2, 'Department': 'R&D', 'Age': 25},
    {'Emp_Id': 'B-45', 'Joining_Date': '15/04/2017', 'Emp_Name': 'Sanika', 'Past_Exp': 5, 'Department': 'Production', 'Age': 27},
    {'Emp_Id': 'C-78', 'Joining_Date': '28/01/2003', 'Emp_Name': 'Shubham', 'Past_Exp': 10, 'Department': 'Maintenance', 'Age': 45},
    {'Emp_Id': 'D-102', 'Joining_Date': '1/9/2010', 'Emp_Name': 'Gaurav', 'Past_Exp': 7, 'Department': 'HR', 'Age': 36},
    {'Emp_Id': 'E-96', 'Joining_Date': '12/5/2022', 'Emp_Name': 'Namita', 'Past_Exp': 1, 'Department': 'Marketing', 'Age': 'NA'},
    {'Emp_Id': 'F-53', 'Joining_Date': '19/02/2008', 'Emp_Name': 'Sushma', 'Past_Exp': 8, 'Department': 'NA', 'Age': 30},
    {'Emp_Id': 'A-12', 'Joining_Date': '11/12/2021', 'Emp_Name': 'Anita', 'Past_Exp': 2, 'Department': 'R&D', 'Age': 25}
]

# Show list of dictionaries
print(Emp_data)

Output: 👇️

[{'Emp_Id': 'A-12', 'Joining_Date': '11/12/2021', 'Emp_Name': 'Anita', 'Past_Exp': 2, 'Department': 'R&D', 'Age': 25}, {'Emp_Id': 'B-45', 'Joining_Date': '15/04/2017', 'Emp_Name': 'Sanika', 'Past_Exp': 5, 'Department': 'Production', 'Age': 27}, {'Emp_Id': 'C-78', 'Joining_Date': '28/01/2003', 'Emp_Name': 'Shubham', 'Past_Exp': 10, 'Department': 'Maintenance', 'Age': 45}, {'Emp_Id': 'D-102', 'Joining_Date': '1/9/2010', 'Emp_Name': 'Gaurav', 'Past_Exp': 7, 'Department': 'HR', 'Age': 36}, {'Emp_Id': 'E-96', 'Joining_Date': '12/5/2022', 'Emp_Name': 'Namita', 'Past_Exp': 1, 'Department': 'Marketing', 'Age': 'NA'}, {'Emp_Id': 'F-53', 'Joining_Date': '19/02/2008', 'Emp_Name': 'Sushma', 'Past_Exp': 8, 'Department': 'NA', 'Age': 30}, {'Emp_Id': 'A-12', 'Joining_Date': '11/12/2021', 'Emp_Name': 'Anita', 'Past_Exp': 2, 'Department': 'R&D', 'Age': 25}]

In this example, we use [] square brackets and {} curly braces to create a list of dictionaries Emp_data. The output shows the list of dictionaries created using this method.

Using append() Function

We can use the append() function to create a list of dictionaries.

Suppose we have the following data:

# Declare empty list of dictionaries
Emp_data = []

# Append dictionaries to list
Emp_data.append({'Emp_Id': 'A-12', 'Joining_Date': '11/12/2021', 'Emp_Name': 'Anita', 'Past_Exp': 2, 'Department': 'R&D', 'Age': 25})
Emp_data.append({'Emp_Id': 'B-45', 'Joining_Date': '15/04/2017', 'Emp_Name': 'Sanika', 'Past_Exp': 5, 'Department': 'Production', 'Age': 27})

# Show list of dictionaries
print(Emp_data)

Output: 👇️

[{'Emp_Id': 'A-12', 'Joining_Date': '11/12/2021', 'Emp_Name': 'Anita', 'Past_Exp': 2, 'Department': 'R&D', 'Age': 25}, {'Emp_Id': 'B-45', 'Joining_Date': '15/04/2017', 'Emp_Name': 'Sanika', 'Past_Exp': 5, 'Department': 'Production', 'Age': 27}]

In this example, we use the append() function to add dictionaries to the list Emp_data.

The output shows the list of dictionaries created using the append() function.

Conclusion

We can use [] square brackets along with {} curly braces or the append() function to create a list of dictionaries in Python.