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

Sort a dictionary by value

Sort a dictionary by value

EasyDictionary Programs · 12 of 94

Write a Python program to sort a dictionary by its values.

Input
{"d": 14, "b": 52, "a": 13, "c": 1}
Output
[('c', 1), ('a', 13), ('d', 14), ('b', 52)]
{'b': 52, 'd': 14, 'a': 13, 'c': 1}

Approach

  1. key=lambda kv: kv[1].

Solution

Python
d = {"d": 14, "b": 52, "a": 13, "c": 1}print(sorted(d.items(), key=lambda kv: kv[1]))print(dict(sorted(d.items(), key=lambda kv: kv[1], reverse=True)))
Output
[('c', 1), ('a', 13), ('d', 14), ('b', 52)]
{'b': 52, 'd': 14, 'a': 13, 'c': 1}

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Sort a dictionary by key

Sort a dictionary by key

EasyDictionary Programs · 11 of 94

Write a Python program to sort a dictionary by its keys.

Input
{"d": 21, "b": 53, "a": 13, "c": 41}
Output
('a', 13)
('b', 53)
('c', 41)
('d', 21)
{'a': 13, 'b': 53, 'c': 41, 'd': 21}

Approach

  1. sorted(d.items()) sorts the pairs by key.

Solution

Python
d = {"d": 21, "b": 53, "a": 13, "c": 41}for pair in sorted(d.items()):    print(pair)print(dict(sorted(d.items())))
Output
('a', 13)
('b', 53)
('c', 41)
('d', 21)
{'a': 13, 'b': 53, 'c': 41, 'd': 21}

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Character counts of a string

Character counts of a string

EasyDictionary Programs · 10 of 94

Write a Python program to build a dictionary of character counts from a string.

Input
"SQATools"
Output
{'S': 1, 'Q': 1, 'A': 1, 'T': 1, 'o': 2, 'l': 1, 's': 1}

Approach

  1. get(ch, 0) + 1 for each character.

Solution

Python
text = "SQATools"counts = {}for ch in text:    counts[ch] = counts.get(ch, 0) + 1print(counts)
Output
{'S': 1, 'Q': 1, 'A': 1, 'T': 1, 'o': 2, 'l': 1, 's': 1}

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs