Print the keys

Print the keys

EasyDictionary Programs · 70 of 94

Write a Python program to print the keys of a dictionary, capitalised.

Input
{"pepsi": 50, "sprite": 60, "slice": 55}
Output
Pepsi
Sprite
Slice

Approach

  1. Loop over the dict.

Solution

Python
drinks = {"pepsi": 50, "sprite": 60, "slice": 55}for name in drinks:    print(name.title())
Output
Pepsi
Sprite
Slice

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Remove keys whose values contain a substring

Remove keys whose values contain a substring

EasyDictionary Programs · 69 of 94

Write a Python program to remove entries whose value contains any of the given substrings.

Input
{1: "sqatools is best", 2: "for learning python"}, ["best", "excellent"]
Output
{2: 'for learning python'}

Approach

  1. Keep values where no substring is present.

Solution

Python
d = {1: "sqatools is best", 2: "for learning python"}subs = ["best", "excellent"]print({k: v for k, v in d.items() if not any(s in v for s in subs)})
Output
{2: 'for learning python'}

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Remove keys with large values

Remove keys with large values

EasyDictionary Programs · 68 of 94

Write a Python program to remove keys whose values are greater than n.

Input
{"sqa": 3, "tools": 5, "python": 7}, n = 6
Output
{'sqa': 3, 'tools': 5}

Approach

  1. Keep v <= n.

Solution

Python
d = {"sqa": 3, "tools": 5, "python": 7}n = 6print({k: v for k, v in d.items() if v <= n})
Output
{'sqa': 3, 'tools': 5}

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Values for keys found in a list

Values for keys found in a list

EasyDictionary Programs · 67 of 94

Write a Python program to get the values of dictionary keys that are also in a list.

Input
["sqatools", "is", "best"], {"sqatools": 10}
Output
[10]

Approach

  1. Loop over the list and look up keys that exist.

Solution

Python
words = ["sqatools", "is", "best"]d = {"sqatools": 10}print([d[w] for w in words if w in d])
Output
[10]

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Nested dictionary from a list of keys

Nested dictionary from a list of keys

EasyDictionary Programs · 66 of 94

Write a Python program to nest each item of a dictionary under a key from a list.

Input
{"sqatools": 8, "python": 6}, [1, 2]
Output
{1: {'sqatools': 8}, 2: {'python': 6}}

Approach

  1. zip the list with the dict’s items.

Solution

Python
d = {"sqatools": 8, "python": 6}keys = [1, 2]print({k: {name: v} for k, (name, v) in zip(keys, d.items())})
Output
{1: {'sqatools': 8}, 2: {'python': 6}}

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Depth of a nested dictionary

Depth of a nested dictionary

MediumDictionary Programs · 65 of 94

Write a Python program to find the depth of a nested dictionary.

Input
{"a": 1, "b": {"c": {"d": {}}}}
Output
4

Approach

  1. Depth is 1 plus the deepest child dictionary’s depth (recursion).

Solution

Python
def depth(d):    if not isinstance(d, dict) or not d:        return 1 if isinstance(d, dict) else 0    return 1 + max(depth(v) for v in d.values()) print(depth({"a": 1, "b": {"c": {"d": {}}}}))
Output
4

Line by line

Lines 2-3

An empty dict has depth 1; a non-dict value adds nothing.

Line 4

Each level adds 1 to the deepest value below it.

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Length of each value

Length of each value

EasyDictionary Programs · 64 of 94

Write a Python program to map each string value to its length.

Input
{1: "sqa", 2: "tools", 3: "python"}
Output
{'sqa': 3, 'tools': 5, 'python': 6}

Approach

  1. {v: len(v) for v in d.values()}.

Solution

Python
d = {1: "sqa", 2: "tools", 3: "python"}print({v: len(v) for v in d.values()})
Output
{'sqa': 3, 'tools': 5, 'python': 6}

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Values for one key from a list of dicts

Values for one key from a list of dicts

EasyDictionary Programs · 63 of 94

Write a Python program to extract the values for a key from a list of dictionaries.

Input
[{"t20": 50, "odi": 70}, {"t20": 40, "odi": 10}, {"t20": 30, "odi": 0}, {"t20": 45, "odi": 65}], key "odi"
Output
[70, 10, 0, 65]

Approach

  1. [d["odi"] for d in data].

Solution

Python
data = [{"t20": 50, "odi": 70}, {"t20": 40, "odi": 10}, {"t20": 30, "odi": 0}, {"t20": 45, "odi": 65}]print([d["odi"] for d in data])
Output
[70, 10, 0, 65]

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Update list values

Update list values

EasyDictionary Programs · 62 of 94

Write a Python program to add 10 to every number in the first list and subtract 25 from the second.

Input
{"virat": [50, 30], "rohit": [40, 10]}
Output
{'virat': [60, 40], 'rohit': [15, -15]}

Approach

  1. Rebuild each list with a comprehension.

Solution

Python
d = {"virat": [50, 30], "rohit": [40, 10]}d["virat"] = [x + 10 for x in d["virat"]]d["rohit"] = [x - 25 for x in d["rohit"]]print(d)
Output
{'virat': [60, 40], 'rohit': [15, -15]}

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Empty every list value

Empty every list value

EasyDictionary Programs · 61 of 94

Write a Python program to clear the list values in a dictionary.

Input
{"virat": [50, 30], "rohit": [40, 10]}
Output
{'virat': [], 'rohit': []}

Approach

  1. clear() each list in place.

Solution

Python
d = {"virat": [50, 30], "rohit": [40, 10]}for v in d.values():    v.clear()print(d)
Output
{'virat': [], 'rohit': []}

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs