Python dictionary inside list (Insertion, Update , retrieval and Delete)

Python dictionary inside list (Insertion, Update , retrieval and Delete)

python dictionary inside list can be created using append(), insert() or while declaring or initialization of the element. To Insert, update, or retrieval process of a Python dictionary is identical to an ordinary element. In this article, we will create a list of dictionaries and then practically show how to Insertion, Update, and retrieve it. So let’s start.

 

python dictionary inside list ( CRUD Operations ) –

Let’s create list of Python dictionaries.

1. Creation –

final_list=[{ "key1": 1}, 
            { "key2": 2} ]

Here we have created list of dictionaries with two sample dicts. now we will see other CRUD operations.

2. insert –

Suppose we want to append a new element to the list. I mean some other dict as a member element. We can simply append as we do ordinary elements like int, str, and float. Here is the code sample.

final_list=[{ "key1": 1}, 
            { "key2": 2} ]

new_dict= { "key3": 3}     
final_list.append(new_dict)
print(final_list)

Now let’s run the above code and see the output.

python dictionary inside list -insert
python dictionary inside list -insert

3. Retrieve & Update –

To update any key of any dict of inside the list we need to first retrieve and update.  Here is the code for this.

final_list=[{ "key1": 1}, 
            { "key2": 2} ]
final_list[1]["key2"]=4
print(final_list)
python dictionary inside list update
python dictionary inside list update

Here we have retrieved the required dictionary and for that, we need to access it as a list element. The same process we need to adopt in the case of a nested dictionary. The fundamentals will always be the same. First, traverse and then update.

4. Delete –

The delete operation also works on the same principle. Let me explain. Suppose I want to delete any dictionary inside the list we need to first traverse and then we need to delete. This operation is more similar to the above update operation. Let me show you.

final_list=[{ "key1": 1}, 
            { "key2": 2} ]
del final_list[0]
print(final_list)

Here we have removed the dictionary which was occurring at the 0th index. The rest operation is the same as the general operation in python list.

Thanks
Data Science Learner Team

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Abhishek ( Chief Editor) , a data scientist with major expertise in NLP and Text Analytics. He has worked on various projects involving text data and have been able to achieve great results. He is currently manages Datasciencelearner.com, where he and his team share knowledge and help others learn more about data science.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner