List of Dictionaries in Python

In this tutorial, we will see about Python’s list of dictionaries ie. a dictionary inside a list, and how to manage its data.

In Python, the basic data structures include lists, sets, tuples, and dictionaries. Each of the data structures is unique in its own way. You can read more about these data structures on this page.

What is Python List?

Lists are one of the simple and most commonly used data structures provided in python. Lists are heterogeneous in nature, which means they can store multiple data types at the same time like string, int, boolean, etc.

Lists are mutable which means the data inside the list can be altered. Lists contain ordered data which is iterable and can also contain duplicate values.

Creating a list and printing its data

list = ['Python', 'Javascript', 'HTML']
print(list)
Output: [‘Python’, ‘Javascript’, ‘HTML’]

 Accessing Items on a list

As lists are iterable, we can access any element(s) of a list through its index value. In Python, counting starts from 0. So to get the first element of the list we use the index 0 so on and so forth.

list = ["Python", 30, "Days", "Learning"]
print(list[2])
Output: Days

What is Python Dictionary?

The dictionary is slightly different from other data structures like lists, sets, and tuples. In a dictionary, data are stored in the form of key-value pairs. Keys inside the dictionaries should be unique whereas their value(s) need not necessarily be unique. Any data inside the dictionary can be accessed through its key.

Creating and printing data from a dictionary

dicti = {
  'brand': 'DELL',
  'model': 'XPS 15',
  'price': 2000
}
print(dicti)
Output: {‘brand’ : ‘DELL’, ‘model’ : ‘XPS 15’, ‘price’ : 2000}

Accessing Items from a dictionary

Since data in a dictionary is stored in the form of keys and their associated values, we can access the value by referring its key:

dicti = {
  'brand': 'DELL',
  'model': 'XPS 15',
  'price': 2000
}

x = dicti.get('price')
print(x)
Output: 2000

List of Dictionaries in Python

As we have seen above, a list can have any elements in it either a string or an integer value. Imagine an individual Python dictionary as an item of a list. This is called a list of dictionaries. Let’s see this:

list = [{
  'brand': 'DELL',
  'model': 'XPS 15',
  'price': 2000
}, {
  'brand': 'HP',
  'model': 'OMEN',
  'price': 3000
}, {
  'brand': 'MAC',
  'model': 'PRO',
  'price': 4000
}]

print(list)
Output: [{‘price’: 2000, ‘brand’: ‘DELL’, ‘model’: ‘XPS 15’}, {‘price’: 3000, ‘brand’: ‘HP’, ‘model’: ‘OMEN’}, {‘price’: 4000, ‘brand’: ‘MAC’, ‘model’: ‘PRO’}]

Getting a particular element (dictionary) from the list using its index value:

list = [{
  'brand': 'DELL',
  'model': 'XPS 15',
  'price': 2000
}, {
  'brand': 'HP',
  'model': 'OMEN',
  'price': 3000
}, {
  'brand': 'MAC',
  'model': 'PRO',
  'price': 4000
}]

print(list[1])
Output: {‘price’: 3000, ‘brand’: ‘HP’, ‘model’: ‘OMEN’}

Fetching a particular value using its keys from a particular dictionary of the list:

list = [{
  'brand': 'DELL',
  'model': 'XPS 15',
  'price': 2000
}, {
  'brand': 'HP',
  'model': 'OMEN',
  'price': 3000
}, {
  'brand': 'MAC',
  'model': 'PRO',
  'price': 4000
}]

print(list[2].get('price'))
Output: 4000