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

Merge two dictionaries with **

Merge two dictionaries with **

EasyDictionary Programs · 14 of 94

Write a Python program to concatenate two dictionaries using unpacking.

Input
{"name": "yash", "city": "pune"}, {"course": "python", "institute": "sqatools"}
Output
{'name': 'yash', 'city': 'pune', 'course': 'python', 'institute': 'sqatools'}

Approach

  1. {d1, d2} works on all Python 3 versions.

Solution

Python
d1 = {"name": "yash", "city": "pune"}d2 = {"course": "python", "institute": "sqatools"}print({**d1, **d2})
Output
{'name': 'yash', 'city': 'pune', 'course': 'python', 'institute': 'sqatools'}

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

Remove duplicate values

Remove duplicate values

EasyDictionary Programs · 9 of 94

Write a Python program to keep only the first key for each distinct value.

Input
{"a": 12, "b": 2, "c": 12, "d": 5, "e": 35, "f": 5}
Output
{'a': 12, 'b': 2, 'd': 5, 'e': 35}

Approach

  1. Remember the values seen while copying items.

Solution

Python
d = {"a": 12, "b": 2, "c": 12, "d": 5, "e": 35, "f": 5}seen = set()result = {}for k, v in d.items():    if v not in seen:        seen.add(v)        result[k] = vprint(result)
Output
{'a': 12, 'b': 2, 'd': 5, 'e': 35}

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Clear a dictionary

Clear a dictionary

EasyDictionary Programs · 8 of 94

Write a Python program to remove all items from a dictionary.

Input
{"a": 1, "b": 2}
Output
{} {}

Approach

  1. clear() empties the same object.

Solution

Python
d = {"a": 1, "b": 2}alias = dd.clear()print(d, alias)
Output
{} {}

Line by line

Line 3

clear empties the dict itself, so every name pointing to it sees the change. d = {} would only rebind d.

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs