Skip to content

Easy Dict

py_simple.easy_dict

Beginner-friendly helpers for common dictionary operations.

count_values(dictionary)

Counts how many times each value appears in the dictionary.

Parameters:

Name Type Description Default
dictionary dict

Dictionary to examine.

required

Returns:

Name Type Description
dict dict

Values mapped to how often they appear.

Example
from py_simple import count_values

result = count_values({"a": 1, "b": 2, "c": 1})
# -> {1: 2, 2: 1}
dictionary = {"a": 1, "b": 2, "c": 1}
result = {}
for value in dictionary.values():
    result[value] = result.get(value, 0) + 1

find_keys(needle, dictionary)

Returns every key whose value matches the given needle.

Parameters:

Name Type Description Default
needle object

Value to look for.

required
dictionary dict

Dictionary to search in.

required

Returns:

Name Type Description
list list

Keys with a matching value.

Example
from py_simple import find_keys

result = find_keys(1, {"a": 1, "b": 2, "c": 1})  # -> ["a", "c"]
needle, dictionary = 1, {"a": 1, "b": 2, "c": 1}
result = [key for key, value in dictionary.items() if value == needle]

get_nested_value(dictionary, path, default=None)

Returns a value from a nested dictionary using a dot-separated path.

Parameters:

Name Type Description Default
dictionary dict

Dictionary to search in.

required
path str

Dot-separated keys, e.g. "user.name".

required
default object

Value to return if the path is missing.

None

Returns:

Name Type Description
object object

The value found, or default.

Example
from py_simple import get_nested_value

data = {"user": {"name": "Ana"}}
result = get_nested_value(data, "user.name")  # -> "Ana"
data = {"user": {"name": "Ana"}}
result = data.get("user", {}).get("name", None)

invert_dict(dictionary)

Returns a new dictionary with keys and values swapped.

Parameters:

Name Type Description Default
dictionary dict

Dictionary to invert.

required

Returns:

Name Type Description
dict dict

Inverted dictionary.

Raises:

Type Description
ValueError

If a value appears more than once and can't be a key.

Example
from py_simple import invert_dict

result = invert_dict({"a": 1, "b": 2})  # -> {1: "a", 2: "b"}
dictionary = {"a": 1, "b": 2}
result = {value: key for key, value in dictionary.items()}

lists_to_dict(keys, values)

Combines two lists into a dictionary, pairing them by position.

Parameters:

Name Type Description Default
keys list

List of keys.

required
values list

List of values.

required

Returns:

Name Type Description
dict dict

Dictionary with keys matched to values.

Raises:

Type Description
ValueError

If keys and values have different lengths.

Example
from py_simple import lists_to_dict

result = lists_to_dict(["name", "age"], ["Ana", 25])
# -> {"name": "Ana", "age": 25}
keys, values = ["name", "age"], ["Ana", 25]
result = dict(zip(keys, values))

merge_dicts(dict_a, dict_b)

Combines two dictionaries into one.

If both dictionaries have the same key, the value from dict_b is kept.

Parameters:

Name Type Description Default
dict_a dict

First dictionary.

required
dict_b dict

Second dictionary.

required

Returns:

Name Type Description
dict dict

Combined dictionary.

Example
from py_simple import merge_dicts

result = merge_dicts({"a": 1}, {"b": 2})  # -> {"a": 1, "b": 2}
dict_a, dict_b = {"a": 1}, {"b": 2}
result = dict_a.copy()
result.update(dict_b)

most_common_value(dictionary)

Returns the value that appears most often in the dictionary.

Parameters:

Name Type Description Default
dictionary dict

Dictionary to examine.

required

Returns:

Name Type Description
object object

The most frequent value.

Raises:

Type Description
ValueError

If the dictionary is empty.

Example
from py_simple import most_common_value

result = most_common_value({"a": 1, "b": 2, "c": 1})  # -> 1
dictionary = {"a": 1, "b": 2, "c": 1}
counts = {}
for value in dictionary.values():
    counts[value] = counts.get(value, 0) + 1
result = max(counts, key=counts.get)

rename_key(dictionary, old_key, new_key)

Returns a copy of the dictionary with one key renamed.

Parameters:

Name Type Description Default
dictionary dict

Dictionary to copy.

required
old_key str

Key to rename.

required
new_key str

New name for the key.

required

Returns:

Name Type Description
dict dict

Copy with the key renamed.

Raises:

Type Description
KeyError

If old_key is not in the dictionary.

ValueError

If new_key already exists in the dictionary.

Example
from py_simple import rename_key

result = rename_key({"name": "Ana"}, "name", "username")
# -> {"username": "Ana"}
dictionary = {"name": "Ana"}
result = dictionary.copy()
result["username"] = result.pop("name")

sort_dict_by_key(dictionary, reverse=False)

Returns a new dictionary with keys sorted alphabetically.

Parameters:

Name Type Description Default
dictionary dict

Dictionary to sort.

required
reverse bool

Sort in descending order if True.

False

Returns:

Name Type Description
dict dict

Dictionary sorted by key.

Example
from py_simple import sort_dict_by_key

result = sort_dict_by_key({"b": 2, "a": 1})  # -> {"a": 1, "b": 2}
dictionary = {"b": 2, "a": 1}
result = {key: dictionary[key] for key in sorted(dictionary)}

sort_dict_by_value(dictionary, reverse=False)

Returns a new dictionary with values sorted from smallest to largest.

Parameters:

Name Type Description Default
dictionary dict

Dictionary to sort.

required
reverse bool

Sort in descending order if True.

False

Returns:

Name Type Description
dict dict

Dictionary sorted by value.

Example
from py_simple import sort_dict_by_value

result = sort_dict_by_value({"a": 2, "b": 1})  # -> {"b": 1, "a": 2}
dictionary = {"a": 2, "b": 1}
result = dict(sorted(dictionary.items(), key=lambda item: item[1]))