Replace marks with their average

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.

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}

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Sort items in descending order of value

Sort items in descending order of value

EasyDictionary Programs · 46 of 94

Write a Python program to sort a dictionary’s items by value, highest first.

Input
{"Math": 70, "Physics": 90, "Chemistry": 67}
Output
{'Physics': 90, 'Math': 70, 'Chemistry': 67}

Approach

  1. sorted(..., key=..., reverse=True).

Solution

Python
d = {"Math": 70, "Physics": 90, "Chemistry": 67}print(dict(sorted(d.items(), key=lambda kv: kv[1], reverse=True)))
Output
{'Physics': 90, 'Math': 70, 'Chemistry': 67}

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Count items in list values

Count items in list values

EasyDictionary Programs · 45 of 94

Write a Python program to count the total number of items in all list values.

Input
{"virat": ["match1", "match2", "match3"], "rohit": ["match1", "match2"]}
Output
5

Approach

  1. sum(len(v) for v in d.values()).

Solution

Python
d = {"virat": ["match1", "match2", "match3"], "rohit": ["match1", "match2"]}print(sum(len(v) for v in d.values()))
Output
5

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

List of dicts to keys and values

List of dicts to keys and values

EasyDictionary Programs · 44 of 94

Write a Python program to convert a list of dictionaries into a list of its keys and a list of its values.

Input
[{"sqa": 123, "tools": 456}]
Output
[['sqa', 'tools'], [123, 456]]

Approach

  1. Collect keys and values separately.

Solution

Python
data = [{"sqa": 123, "tools": 456}]keys = [k for d in data for k in d]values = [v for d in data for v in d.values()]print([keys, values])
Output
[['sqa', 'tools'], [123, 456]]

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Dictionary of lists to list of lists

Dictionary of lists to list of lists

EasyDictionary Programs · 43 of 94

Write a Python program to convert {key: [values]} into [[key, values…], …].

Input
{"sqa": [1, 4, 6], "tools": [3, 6, 9]}
Output
[['sqa', 1, 4, 6], ['tools', 3, 6, 9]]

Approach

  1. [k, *v] unpacks the list after the key.

Solution

Python
d = {"sqa": [1, 4, 6], "tools": [3, 6, 9]}print([[k, *v] for k, v in d.items()])
Output
[['sqa', 1, 4, 6], ['tools', 3, 6, 9]]

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Print a nested dictionary line by line

Print a nested dictionary line by line

EasyDictionary Programs · 42 of 94

Write a Python program to print a nested dictionary line by line.

Input
{"virat": {"sport": "cricket", "team": "india"}, "messi": {"sport": "football", "team": "argentina"}}
Output
Virat
  Sport : cricket
  Team : india
Messi
  Sport : football
  Team : argentina

Approach

  1. An outer loop for players, an inner loop for their details.

Solution

Python
d = {"virat": {"sport": "cricket", "team": "india"},     "messi": {"sport": "football", "team": "argentina"}}for name, info in d.items():    print(name.title())    for key, value in info.items():        print(f"  {key.title()} : {value}")
Output
Virat
  Sport : cricket
  Team : india
Messi
  Sport : football
  Team : argentina

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Product with the highest price

Product with the highest price

EasyDictionary Programs · 41 of 94

Write a Python program to find the product with the highest price.

Input
{"price1": 450, "price2": 600, "price3": 255, "price4": 400}
Output
price2 600

Approach

  1. max(d, key=d.get).

Solution

Python
d = {"price1": 450, "price2": 600, "price3": 255, "price4": 400}best = max(d, key=d.get)print(best, d[best])
Output
price2 600
Note

The highest price is 600 for price2; the original sample output said 500.

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Sort the list values

Sort the list values

EasyDictionary Programs · 40 of 94

Write a Python program to sort each list value of a dictionary.

Input
{"a1": [1, 5, 3], "a2": [10, 6, 20]}
Output
{'a1': [1, 3, 5], 'a2': [6, 10, 20]}

Approach

  1. {k: sorted(v) ...}.

Solution

Python
d = {"a1": [1, 5, 3], "a2": [10, 6, 20]}print({k: sorted(v) for k, v in d.items()})
Output
{'a1': [1, 3, 5], 'a2': [6, 10, 20]}

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

List to a nested dictionary

List to a nested dictionary

MediumDictionary Programs · 39 of 94

Write a Python program to convert a list into a nested dictionary of keys.

Input
["a", "b", "c", "d"]
Output
{'a': {'b': {'c': {'d': {}}}}}

Approach

  1. Build from the inside out: start with {} and wrap it for each key in reverse.

Solution

Python
keys = ["a", "b", "c", "d"]nested = {}for key in reversed(keys):    nested = {key: nested}print(nested)
Output
{'a': {'b': {'c': {'d': {}}}}}

Line by line

Lines 3-4

Starting from “d” and wrapping outwards gives {“a”: {“b”: {“c”: {“d”: {}}}}}.

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Mean of dictionary values

Mean of dictionary values

EasyDictionary Programs · 38 of 94

Write a Python program to find the mean of a dictionary’s values.

Input
{"m1": 25, "m2": 20, "m3": 15}
Output
Mean is 20.0

Approach

  1. sum / len.

Solution

Python
d = {"m1": 25, "m2": 20, "m3": 15}print("Mean is", sum(d.values()) / len(d))
Output
Mean is 20.0

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs