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

Memory size of a dictionary

Memory size of a dictionary

EasyDictionary Programs · 17 of 94

Write a Python program to get the memory size of a dictionary with sys.getsizeof, and its number of items.

Input
{"name": "virat", "sport": "cricket"}
Output
2 items
True | bytes on this Python: 184

Approach

  1. sys.getsizeof gives the size of the dict object itself, len the item count.

Solution

Python
import sys d = {"name": "virat", "sport": "cricket"}print(len(d), "items")print(sys.getsizeof(d) > 0, "| bytes on this Python:", sys.getsizeof(d))
Output
2 items
True | bytes on this Python: 184
Note

The byte count depends on the Python version and platform, and it does not include the keys and values themselves.

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Sum of dictionary values

Sum of dictionary values

EasyDictionary Programs · 16 of 94

Write a Python program to add up all the values of a dictionary.

Input
{"x": 23, "y": 10, "z": 7}
Output
40

Approach

  1. sum(d.values()).

Solution

Python
print(sum({"x": 23, "y": 10, "z": 7}.values()))
Output
40

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Swap the values of two keys

Swap the values of two keys

EasyDictionary Programs · 15 of 94

Write a Python program to swap the values of two keys.

Input
{"name": "yash", "city": "pune"}
Output
{'name': 'pune', 'city': 'yash'}

Approach

  1. Tuple assignment on the two keys.

Solution

Python
d = {"name": "yash", "city": "pune"}d["name"], d["city"] = d["city"], d["name"]print(d)
Output
{'name': 'pune', 'city': 'yash'}

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Add a key

Add a key

EasyDictionary Programs · 13 of 94

Write a Python program to add a key to a dictionary.

Input
{1: "a", 2: "b"}
Output
{1: 'a', 2: 'b', 3: 'c'}

Approach

  1. Assign to the new key.

Solution

Python
d = {1: "a", 2: "b"}d[3] = "c"print(d)
Output
{1: 'a', 2: 'b', 3: 'c'}

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs