Appending a dictionary to a list in Python


Dictionaries allows us to store key-value pairs, while lists allow you to store multiple values in a single variable and one common task is to append a dictionary to a list, which can be useful for storing multiple dictionaries in a single list. In this article, we will explore how to append a dictionary to a list in Python using different methods. We will provide examples and step-by-step instructions on how to do this.

Introduction

Python provides a wide variety of built-in data structures, which makes programming simple and effective. In contrast to other data structures, each of these Python data structures follows a sequential pattern and may store data collections in a variety of forms. Python has a number of data structures, some of which are basic but powerful and can store a large variety of data, for example, the list and the dictionary.

List

Python Lists are identical to dynamically scaled arrays specified in other programming languages (vector in C++ and ArrayList in Java). Python Lists may contain any number of elements. A collection of items, contained in brackets (), and separated from one another using commas is referred to as a list.

The collection of data may be stored in the form of a sequence using the list, which is a sequence data type. There are other data types known as tuples and strings that belong to the sequence category.

Syntax

List= [enter the elements you want in the list]

The elements should be quoted in double quotes and separated from each other using a comma.

Example

sample_list1 = ["List","Example","In","Python"]
print(sample_list1)

Output

['List', 'Example', 'In', 'Python']

Dictionaries

The Python dictionary may be thought of as an ordered collection of elements (starting with Python 3.7). The items are saved as key-value pairs there. In this context, keys denote one-of-a-kind identifiers that are connected to each value.

Putting a list of key-value pairs inside of curly braces () is one way to create a dictionary. The list should be separated by commas. Each key is differentiated from the value that is connected with it by a colon (:).

Syntax

Dict1={<key>: <Value>,…………,………}

Example

sports = {"1": "Cricket", "2": "Volleyball", "3": "Football"}
print(sports)

Output

{'1': 'Cricket', '2': 'Volleyball', '3': 'Football'}

Append a dictionary to a list in Python

Below are some methods using which we can append a dictionary to a list in Python −

Using the append() method

We will be using the append function to append a dictionary to the list.

Syntax

List=list1.append(dictionary)

Example

#list
l1 = [6, 24, "hello", "there"]

# dictionary
city = {
   2024: 'Delhi',
   2021: 'Lucknow',
   7060: 'Hyderabad',
   1034: 'Gurgaon',
   7062: 'Kanpur'
}

# append this dictionary to the empty list
l1.append(city)

# display list
l1

Output

[6,
 24,
 'hello',
 'there',
 {2024: 'Delhi',
  2021: 'Lucknow',
  7060: 'Hyderabad',
  1034: 'Gurgaon',
  7062: 'Kanpur'}]

Using the append() and copy() Method

We will be using the copy function to make the copy of the dictionary and then append it append function to append a dictionary to the list.

Syntax

List=list1.append(dictionary.copy())

Example

#list
l1 = [6, 24, "hello", "there"]

# dictionary
city = {
   2024: 'Delhi',
   2021: 'Lucknow',
   7060: 'Hyderabad',
   1034: 'Gurgaon',
   7062: 'Kanpur'
}

# append this dictionary to the empty list
l1.append(city.copy())

# display list
l1

Output

[6,
 24,
 'hello',
 'there',
 {2024: 'Delhi',
  2021: 'Lucknow',
  7060: 'Hyderabad',
  1034: 'Gurgaon',
  7062: 'Kanpur'}

Using the append() and deepcopy() Method

Here we will be using the deepcopy() function to make the copy of the dictionary recursively and then append it append function to append a dictionary to the list.

Syntax

List=list1.append(dictionary.deepcopy())

Example

#list
l1 = [6, 24, "hello", "there"]

# dictionary
city = {
   2024: 'Delhi',
   2021: 'Lucknow',
   7060: 'Hyderabad',
   1034: 'Gurgaon',
   7062: 'Kanpur'
}

# append this dictionary to the empty list
l1.append(city.deepcopy())

# display list
l1

Output

[6,
 24,
 'hello',
 'there',
 {2024: 'Delhi',
  2021: 'Lucknow',
  7060: 'Hyderabad',
  1034: 'Gurgaon',
  7062: 'Kanpur'}

Using the append() Method with an Empty List

We will be using the append function to append a dictionary to the empty list.

Syntax

List=list1.append()

Example

#create a list
l1 = []

# create a dictionary
city = {
   2024: 'Delhi',
   2021: 'Lucknow',
   7060: 'Hyderabad',
   1034: 'Gurgaon',
   7062: 'Kanpur'
}

# append this dictionary to the empty list
l1.append(city)

# display the list
l1

Output

[
 {2024: 'Delhi',

  2021: 'Lucknow',
  7060: 'Hyderabad',
  1034: 'Gurgaon',
  7062: 'Kanpur'

Conclusion

In this article, we learned about lists, dictionaries with their syntax, and methods to append dictionaries to the list in python. We have learned three methods one is using an append function, append function with copy function, and append function with a deep copy function.

Updated on: 31-May-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements