Convert string values to numbers

Convert string values to numbers

EasyDictionary Programs · 60 of 94

Write a Python program to convert the string values of a dictionary to int or float.

Input
{"a": "30", "b": "20", "c": "10"}, {"a": "3.33", "b": "20.50", "c": "12.5"}
Output
{'a': 30, 'b': 20, 'c': 10}
{'a': 3.33, 'b': 20.5, 'c': 12.5}

Approach

  1. Use int when the text has no decimal point, otherwise float.

Solution

Python
def to_number(text):    return float(text) if "." in text else int(text) for d in [{"a": "30", "b": "20", "c": "10"}, {"a": "3.33", "b": "20.50", "c": "12.5"}]:    print({k: to_number(v) for k, v in d.items()})
Output
{'a': 30, 'b': 20, 'c': 10}
{'a': 3.33, 'b': 20.5, 'c': 12.5}

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Remove a dictionary from a list

Remove a dictionary from a list

EasyDictionary Programs · 59 of 94

Write a Python program to remove the 4th dictionary from a list.

Input
[{"t20": 50, "odi": 70}, {"t20": 40, "odi": 10}, {"t20": 30, "odi": 0}, {"t20": 45, "odi": 65}]
Output
[{'t20': 50, 'odi': 70}, {'t20': 40, 'odi': 10}, {'t20': 30, 'odi': 0}]

Approach

  1. pop(3).

Solution

Python
data = [{"t20": 50, "odi": 70}, {"t20": 40, "odi": 10}, {"t20": 30, "odi": 0}, {"t20": 45, "odi": 65}]data.pop(3)print(data)
Output
[{'t20': 50, 'odi': 70}, {'t20': 40, 'odi': 10}, {'t20': 30, 'odi': 0}]

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Dict of lists to list of dicts

Dict of lists to list of dicts

EasyDictionary Programs · 58 of 94

Write a Python program to split a dictionary of lists into a list of dictionaries.

Input
{"t20": [50, 40, 30, 45], "odi": [70, 10, 0, 65]}
Output
[{'t20': 50, 'odi': 70}, {'t20': 40, 'odi': 10}, {'t20': 30, 'odi': 0}, {'t20': 45, 'odi': 65}]

Approach

  1. zip(*d.values()) gives one row per position.

Solution

Python
d = {"t20": [50, 40, 30, 45], "odi": [70, 10, 0, 65]}print([dict(zip(d, row)) for row in zip(*d.values())])
Output
[{'t20': 50, 'odi': 70}, {'t20': 40, 'odi': 10}, {'t20': 30, 'odi': 0}, {'t20': 45, 'odi': 65}]

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Group key-value pairs into lists

Group key-value pairs into lists

EasyDictionary Programs · 57 of 94

Write a Python program to group a sequence of (key, value) pairs into a dictionary of lists.

Input
[("virat", 50), ("rohit", 40), ("virat", 30), ("rohit", 10)]
Output
{'virat': [50, 30], 'rohit': [40, 10]}

Approach

  1. defaultdict(list).

Solution

Python
from collections import defaultdict pairs = [("virat", 50), ("rohit", 40), ("virat", 30), ("rohit", 10)]groups = defaultdict(list)for k, v in pairs:    groups[k].append(v)print(dict(groups))
Output
{'virat': [50, 30], 'rohit': [40, 10]}
Note

A dict literal cannot hold the same key twice, so the input is a list of pairs.

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Are all values the same?

Are all values the same?

EasyDictionary Programs · 56 of 94

Write a Python program to check whether all values in a dictionary are the same.

Input
{"virat": 50, "rohit": 50, "rahul": 50, "hardik": 50}
Output
True

Approach

  1. A set of the values has one item when they are all equal.

Solution

Python
d = {"virat": 50, "rohit": 50, "rahul": 50, "hardik": 50}print(len(set(d.values())) == 1)
Output
True

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Matrix rows to a dictionary

Matrix rows to a dictionary

EasyDictionary Programs · 55 of 94

Write a Python program to convert a matrix into a dictionary keyed by row number.

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

Approach

  1. enumerate(matrix, start=1).

Solution

Python
matrix = [[1, 2, 3], [4, 5, 6]]print(dict(enumerate(matrix, start=1)))
Output
{1: [1, 2, 3], 2: [4, 5, 6]}

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

String to a dictionary

String to a dictionary

EasyDictionary Programs · 54 of 94

Write a Python program to convert “key=value; key=value” text into a dictionary.

Input
"Apr=April; Mar=March"
Output
{'Apr': 'April', 'Mar': 'March'}

Approach

  1. Split on “;”, then split each part on “=” and strip spaces.

Solution

Python
text = "Apr=April; Mar=March"d = dict(part.strip().split("=") for part in text.split(";"))print(d)
Output
{'Apr': 'April', 'Mar': 'March'}

Line by line

Line 2

strip() removes the space after each semicolon, so the second key is “Mar”, not ” Mar”.

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

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