Replace marks with their average

EasyDictionary Programs · 47 of 94

Write a Python program to replace the numeric marks p1 and p2 with a single average entry.

Advertisement
Input
{"name": "ketan", "subject": "maths", "p1": 80, "p2": 70}
Output
{'name': 'ketan', 'subject': 'maths', 'average': 75.0}

Approach

  1. Pop both marks and store their average under a new key.

Solution

Python
d = {"name": "ketan", "subject": "maths", "p1": 80, "p2": 70}p1, p2 = d.pop("p1"), d.pop("p2")d["average"] = (p1 + p2) / 2print(d)
Output
{'name': 'ketan', 'subject': 'maths', 'average': 75.0}
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More dictionary programs

← All 94 Dictionary Programs