List of tuples to a dictionary

List of tuples to a dictionary

EasyDictionary Programs · 53 of 94

Write a Python program to convert a list of pairs into a dictionary.

Input
[("mike", 1), ("Sarah", 20), ("Jim", 16)]
Output
{'mike': 1, 'Sarah': 20, 'Jim': 16}

Approach

  1. dict(pairs).

Solution

Python
print(dict([("mike", 1), ("Sarah", 20), ("Jim", 16)]))
Output
{'mike': 1, 'Sarah': 20, 'Jim': 16}

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Two lists in a dict to one flat dict

Two lists in a dict to one flat dict

EasyDictionary Programs · 52 of 94

Write a Python program to pair the two list values of a dictionary into a flat dictionary.

Input
{"name": ["Apr", "May", "June"], "month": [4, 5, 6]}
Output
{'Apr': 4, 'May': 5, 'June': 6}

Approach

  1. zip the two lists.

Solution

Python
d = {"name": ["Apr", "May", "June"], "month": [4, 5, 6]}print(dict(zip(d["name"], d["month"])))
Output
{'Apr': 4, 'May': 5, 'June': 6}

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Filter by value

Filter by value

EasyDictionary Programs · 51 of 94

Write a Python program to keep entries with values greater than 40.

Input
{"alex": 50, "john": 45, "Robert": 30}
Output
{'alex': 50, 'john': 45}

Approach

  1. Dict comprehension with a condition.

Solution

Python
d = {"alex": 50, "john": 45, "Robert": 30}print({k: v for k, v in d.items() if v > 40})
Output
{'alex': 50, 'john': 45}

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Drop empty items

Drop empty items

EasyDictionary Programs · 50 of 94

Write a Python program to drop items whose value is None.

Input
{"m1": 40, "m2": 50, "m3": None}
Output
{'m1': 40, 'm2': 50}

Approach

  1. Keep pairs where v is not None.

Solution

Python
d = {"m1": 40, "m2": 50, "m3": None}print({k: v for k, v in d.items() if v is not None})
Output
{'m1': 40, 'm2': 50}

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Keys a, b, c with ranges

Keys a, b, c with ranges

EasyDictionary Programs · 49 of 94

Write a Python program to create {“a”: 1-5, “b”: 6-10, “c”: 11-15} as lists.

Output
{'a': [1, 2, 3, 4, 5], 'b': [6, 7, 8, 9, 10], 'c': [11, 12, 13, 14, 15]}

Approach

  1. Each key’s list starts 5 higher than the previous.

Solution

Python
print({key: list(range(1 + 5 * i, 6 + 5 * i)) for i, key in enumerate("abc")})
Output
{'a': [1, 2, 3, 4, 5], 'b': [6, 7, 8, 9, 10], 'c': [11, 12, 13, 14, 15]}

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Matching key-value pairs

Matching key-value pairs

EasyDictionary Programs · 48 of 94

Write a Python program to find key-value pairs present in both dictionaries.

Input
{"k1": "p", "k2": "q", "k3": "r"}, {"k1": "p", "k2": "s"}
Output
k1: p is present in both a and b

Approach

  1. items() views support &.

Solution

Python
a = {"k1": "p", "k2": "q", "k3": "r"}b = {"k1": "p", "k2": "s"}for k, v in a.items() & b.items():    print(f"{k}: {v} is present in both a and b")
Output
k1: p is present in both a and b

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Replace marks with their average

Replace marks with their average

EasyDictionary Programs · 47 of 94

Write a Python program to replace the numeric marks p1 and p2 with a single average entry.

Input
{"name": "ketan", "subject": "maths", "p1": 80, "p2": 70}
Output
{'name': 'ketan', 'subject': 'maths', 'average': 75.0}

Approach

  1. Pop both marks and store their average under a new key.

Solution

Python
d = {"name": "ketan", "subject": "maths", "p1": 80, "p2": 70}p1, p2 = d.pop("p1"), d.pop("p2")d["average"] = (p1 + p2) / 2print(d)
Output
{'name': 'ketan', 'subject': 'maths', 'average': 75.0}

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Sort items in descending order of value

Sort items in descending order of value

EasyDictionary Programs · 46 of 94

Write a Python program to sort a dictionary’s items by value, highest first.

Input
{"Math": 70, "Physics": 90, "Chemistry": 67}
Output
{'Physics': 90, 'Math': 70, 'Chemistry': 67}

Approach

  1. sorted(..., key=..., reverse=True).

Solution

Python
d = {"Math": 70, "Physics": 90, "Chemistry": 67}print(dict(sorted(d.items(), key=lambda kv: kv[1], reverse=True)))
Output
{'Physics': 90, 'Math': 70, 'Chemistry': 67}

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Count items in list values

Count items in list values

EasyDictionary Programs · 45 of 94

Write a Python program to count the total number of items in all list values.

Input
{"virat": ["match1", "match2", "match3"], "rohit": ["match1", "match2"]}
Output
5

Approach

  1. sum(len(v) for v in d.values()).

Solution

Python
d = {"virat": ["match1", "match2", "match3"], "rohit": ["match1", "match2"]}print(sum(len(v) for v in d.values()))
Output
5

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

List of dicts to keys and values

List of dicts to keys and values

EasyDictionary Programs · 44 of 94

Write a Python program to convert a list of dictionaries into a list of its keys and a list of its values.

Input
[{"sqa": 123, "tools": 456}]
Output
[['sqa', 'tools'], [123, 456]]

Approach

  1. Collect keys and values separately.

Solution

Python
data = [{"sqa": 123, "tools": 456}]keys = [k for d in data for k in d]values = [v for d in data for v in d.values()]print([keys, values])
Output
[['sqa', 'tools'], [123, 456]]

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs