codette
codette

Reputation: 103

Python code to remove the nested key from a value in dictionary

Currently I have a nested dictionary with keys as "Apple" and "Mango". and in the value we have "0" again as keys.

Fruits = {
  "Apple": {
    "0": 0.066
  },
  "Mango": {
    "0": 0.933
  }
}

instead I wish it to look like :

Fruits = {
  "Apple": 0.066
  ,
  "Mango": 0.933
}

Upvotes: 0

Views: 133

Answers (2)

Abdulmajeed
Abdulmajeed

Reputation: 1545

Fruits = {
  "Apple": {
    "0": 0.066
  },
  "Mango": {
    "0": 0.933
  }
}

new_fruits = {}

for fruit, values in Fruits.items():
    new_fruits[fruit] = values["0"]

print(new_fruits)

output:

{'Apple': 0.066, 'Mango': 0.933}

Upvotes: 1

Zero-nnkn
Zero-nnkn

Reputation: 101

Just try:

Fruits = {k: v['0'] for k, v in Fruits.items()}

Upvotes: 2

Related Questions