Group equal items

Group equal items

EasyDictionary Programs · 27 of 94

Write a Python program to group equal items of a list into dictionary values.

Input
[1, 3, 4, 4, 2, 5, 3, 1, 5, 5, 2]
Output
{1: [1, 1], 2: [2, 2], 3: [3, 3], 4: [4, 4], 5: [5, 5, 5]}

Approach

  1. setdefault(x, []).append(x), then sort by key.

Solution

Python
items = [1, 3, 4, 4, 2, 5, 3, 1, 5, 5, 2]groups = {}for x in items:    groups.setdefault(x, []).append(x)print(dict(sorted(groups.items())))
Output
{1: [1, 1], 2: [2, 2], 3: [3, 3], 4: [4, 4], 5: [5, 5, 5]}

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Max and min values

Max and min values

EasyDictionary Programs · 26 of 94

Write a Python program to find the maximum and minimum values of a dictionary, with their keys.

Input
{"a": 10, "b": 44, "c": 60, "d": 25}
Output
Maximum value: 60 at key c
Minimum value: 10 at key a

Approach

  1. max(d, key=d.get) returns the key with the largest value.

Solution

Python
d = {"a": 10, "b": 44, "c": 60, "d": 25}print("Maximum value:", max(d.values()), "at key", max(d, key=d.get))print("Minimum value:", min(d.values()), "at key", min(d, key=d.get))
Output
Maximum value: 60 at key c
Minimum value: 10 at key a

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Map two lists to a dictionary

Map two lists to a dictionary

EasyDictionary Programs · 25 of 94

Write a Python program to map a list of keys to a list of values.

Input
["name", "sport", "rank", "age"], ["Virat", "cricket", 1, 32]
Output
{'name': 'Virat', 'sport': 'cricket', 'rank': 1, 'age': 32}

Approach

  1. dict(zip(...)).

Solution

Python
keys = ["name", "sport", "rank", "age"]values = ["Virat", "cricket", 1, 32]print(dict(zip(keys, values)))
Output
{'name': 'Virat', 'sport': 'cricket', 'rank': 1, 'age': 32}

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Remove a key

Remove a key

EasyDictionary Programs · 24 of 94

Write a Python program to remove a key from a dictionary.

Input
{"a": 2, "b": 4, "c": 5}, remove "c"
Output
5 {'a': 2, 'b': 4}
not found

Approach

  1. pop(key, default) removes safely; del raises KeyError for a missing key.

Solution

Python
d = {"a": 2, "b": 4, "c": 5}removed = d.pop("c")print(removed, d)print(d.pop("zzz", "not found"))
Output
5 {'a': 2, 'b': 4}
not found

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Product of dictionary values

Product of dictionary values

EasyDictionary Programs · 23 of 94

Write a Python program to multiply all the values of a dictionary.

Input
{"a": 2, "b": 4, "c": 5}
Output
40

Approach

  1. math.prod(d.values()).

Solution

Python
import mathprint(math.prod({"a": 2, "b": 4, "c": 5}.values()))
Output
40

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Squares of 1 to 5

Squares of 1 to 5

EasyDictionary Programs · 22 of 94

Write a Python program to create a dictionary with keys 1 to 5 and their squares as values.

Output
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

Approach

  1. Dict comprehension.

Solution

Python
print({k: k * k for k in range(1, 6)})
Output
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Insert a key at the beginning

Insert a key at the beginning

EasyDictionary Programs · 21 of 94

Write a Python program to insert a key at the beginning of a dictionary.

Input
{"course": "python", "institute": "sqatools"}, insert ("name", "omkar")
Output
{'name': 'omkar', 'course': 'python', 'institute': 'sqatools'}

Approach

  1. Dicts keep insertion order, so build a new dict with the new key first.

Solution

Python
d = {"course": "python", "institute": "sqatools"}d = {"name": "omkar", **d}print(d)
Output
{'name': 'omkar', 'course': 'python', 'institute': 'sqatools'}

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Dictionary of cubes

Dictionary of cubes

EasyDictionary Programs · 20 of 94

Write a Python program to create {k: k³} for k from 1 to n.

Input
n = 4
Output
{1: 1, 2: 8, 3: 27, 4: 64}

Approach

  1. A dict comprehension over range(1, n + 1).

Solution

Python
n = 4print({k: k ** 3 for k in range(1, n + 1)})
Output
{1: 1, 2: 8, 3: 27, 4: 64}

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Iterate over a dictionary

Iterate over a dictionary

EasyDictionary Programs · 19 of 94

Write a Python program to loop over a dictionary and print each key and value.

Input
{"food": "burger", "type": "fast food"}
Output
food : burger
type : fast food

Approach

  1. for k, v in d.items().

Solution

Python
d = {"food": "burger", "type": "fast food"}for key, value in d.items():    print(key, ":", value)
Output
food : burger
type : fast food

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Does a key exist?

Does a key exist?

EasyDictionary Programs · 18 of 94

Write a Python program to check whether a key exists in a dictionary.

Input
{"city": "pune", "state": "maharashtra"}, key "country"
Output
country does not exist
city exists

Approach

  1. in checks keys.

Solution

Python
d = {"city": "pune", "state": "maharashtra"}for key in ["country", "city"]:    print(key, "exists" if key in d else "does not exist")
Output
country does not exist
city exists

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs