Remove key and value from nested dictionaries?

remove key from nested dictionary python
python create nested dictionary from list
python json nested dictionaries
python find value in nested dictionary
python get value from nested dictionary
python remove item from dictionary by value
nested dictionary comprehension python
update nested dictionary python

I have a nested dictionary like this:

book={20:{'start':'2008','reason':'sold'},
      21:{'start':'2009','reason':'returned'},
      22:{'start':'2010','reason':'returned'}}

I want to remove items that 'reason'== 'sold' .

The output should be like this:

book={21:{'start':'2009','reason':'returned'},
      22:{'start':'2010','reason':'returned'}}

This is my code which is not working:

for k, v in book:
   if v == 'sold':
       del book[k] 

This gets the job done with some debug stuff.

booko = book.copy()
for k in book:
   print('k', k)
   if 'reason' in book[k] and book[k]['reason'] == 'sold':
       print(book[k]['reason'])
       del booko[k]
print booko
book=booko

Copied the original because otherwise the iterator doesn't know what's next...

The dictionary comprehension is cool, but super weird to grok. I like loops for maintainability. Also as you're learning, you can execute the print statements to see what's actually going on.

Remove keys from a nested dict (Python keys), The idea is to keep the keys (hair.color) with values(blonde). In this example: remove Micheal. Code: def answers(  pop() can be used to delete a key and its value inplace. Advantage over using del is that it provides the mechanism to print desired value if tried to remove a non-existing dict. pair. Second, it also returns the value of key that is being removed in addition to performing a simple delete operation.

You can use a dictionary comprehension:

book={20:{'start':'2008','reason':'sold'},
  21:{'start':'2009','reason':'returned'},
  22:{'start':'2010','reason':'returned'}}
new_book = {a:b for a, b in book.items() if b['reason'] != 'sold'}

Output:

{21: {'start': '2009', 'reason': 'returned'}, 22: {'start': '2010', 'reason': 'returned'}}

Python, print ( "initial dictionary" , str (ini_dict)). # code to remove duplicates. result = {}. for key, value in ini_dict.items():. if value not in result.values():. result[key] = value. Sometimes, while working with Python Dictionaries, we can have nesting of dictionaries, with each key being single values dictionary. In this we need to remove the top level of dictionary. This can have application in data preprocessing. Lets discuss certain ways in which this task can be performed. Method #1 : Using values() + dictionary

try this:

dictionary = {}
book={20:{'start':'2008','reason':'sold'},
      21:{'start':'2009','reason':'returned'},
      22:{'start':'2010','reason':'returned'}}
for d in book:
   if book[d]['reason'] != 'sold':
      dictionary[d] = book[d]
   else:
      pass
print(dictionary)

Output:

{21: {'start': '2009', 'reason': 'returned'}, 22: {'start': '2010', 'reason': 'returned'}}

I iterate trough the book dictionary and then i check if 'reason' column of the dictionary is 'sold' or not, if it is, pass, if it's not add it to the new dictionary

Python, This article deals with one such task of deleting a dictionary key-value pair from a dictionary. Code #1 : Demonstrating key-value pair deletion using del dictionaries from nested dictionary · Python | Ways to remove particular list element  In Python, a dictionary is an unordered collection of items. Here, dictionary has a key:value pair enclosed within curly brackets {}. To learn more about dictionary, please visit Python Dictionary. In Python, a nested dictionary is a dictionary inside a dictionary. It's a collection of dictionaries into one single dictionary.

Python Nested Dictionary (With Examples), In this example, I want to remove a remove: "me" item from the full_dict.outer_dict.​inner_dict dictionary: full_dict: outer_key: "outer key value" outer_dict: Given a nested dictionary, the task is to remove duplicate dictionaries from the dictionary. Given below are few methods to complete the given task. Method #1: Using Naive Method.

Removing a deeply-nested dict item in Ansible, Removing an item from a dictionary; Adding new key-values to a Accessing elements in a nested dictionary; Deleting elements from a nested  Remove a key from dictionary using dict.pop() If key exists in dictionary then dict.pop() removes the element with given key from dictionary and return its value. If given key doesn’t exist in dictionary then it returns the given Default value.

Dictionaries in Python · Pylenin, There are several ways to remove items from a nested dictionary. It removes the key and returns its value. Python Exercises, Practice and Solution: Write a Python program to remove key values pairs from a list of dictionaries.

Comments
  • 1. What does happen? 2. Try printing "book[k]" instead of attempting to delete it. What gets printed?
  • Thanks for your answer. I get this error: 'KeyError: 'reason'' . @Jonathan
  • try it now. seems you don't have the 'reason' key in all the dicts.
  • Perfect! Yes, I don't have 'reason' in all the dicts. I didn't notice that. Thank you so much.