In this lesson we want to learn How to create list of dictionary in Python ? Python is high level, interpreted and general purpose programming language. it was created in late 1980s by Guido van Rossum and was first released in 1991. Python is known for its clear and concise syntax and it makes it easy to read and write. It is dynamically typed it means that you don’t need to declare the data type of variable before using it and also it supports multiple programming paradigms, including object oriented, procedural and functional programming.
Python is widely used in different types of applications including web development, scientific computing, data analysis, artificial intelligence, and many more. also popular language for beginners due to its simplicity and readability and it makes great choice for educational purposes.
How to create list of dictionary in Python ?
List of dictionaries is data structure that allows you to store multiple dictionaries in single list. In Python, you can create list of dictionaries by creating list and adding dictionaries to it. this is an example:
1 2 3 |
list_of_dicts = [{'name': 'John', 'age': 30},{'name': 'Jane', 'age': 25},{'name': 'Jim', 'age': 35}] print(list_of_dicts) |
In this example list of dictionaries are created using square brackets []
. each dictionary in the list is created using curly braces {}
. the keys and values of each dictionary are separated by colons :
, and each key value pair is separated by comma ,
.
you can access individual dictionaries within the list using their index. for example, you can access the first dictionary in the list like this:
1 2 |
first_dict = list_of_dicts[0] print(first_dict) |
Output:
1 |
{'name': 'John', 'age': 30} |
You can also access individual values within dictionary using the key. for example, you can access the value of the name key in the first dictionary like this:
1 2 |
name = list_of_dicts[0]['name'] print(name) |
Output:
1 |
John |