Skip to main content

Applies a given function to the values of a nested dictionary

Project description

Applies a given function to the values of a nested dictionary

Tested against Windows 10 / Python 3.10 / Anaconda

pip install functionapplydict

apply_function_dict(d: dict, fu=lambda keys, item, d: item):
    Apply a given function to the values of a nested dictionary and return a modified dictionary.

    Parameters:
    - d (dict): The input dictionary to be modified.
    - fu (function): The function to be applied to each value in the dictionary.
        - keys (list): The list of keys leading to the current value.
        - item (any): The current value in the dictionary.
        - d (dict): The input dictionary.

    Returns:
    - dict: A new dictionary with the same structure as the input, but with modified values.

    Example:
        from functionapplydict import apply_function_dict

        child1 = {"name": "Emil", "year": 2004}
        child2 = {"name": "Tobias", "year": 2007}
        child3 = {"name": "Linus", "year": 2011}

        myfamily = {"child1": child1, "child2": child2, "child3": child3}


        def function1(keys, item, d):
            if isinstance(item, int):
                item = item * 8
            return item


        dnew = apply_function_dict(d=myfamily, fu=function1)
        dnew2 = apply_function_dict(d=myfamily, fu=lambda keys, item, d: str(item))

        ditest = {1: {2: [4, 56, 2, 4, 56]}, 2: {24: [4, 56, 2, 4, 56, 444]}}
        dnew3 = apply_function_dict(d=ditest, fu=lambda keys, item, d: sum(item))
        dnew4 = apply_function_dict(d=ditest, fu=lambda keys, item, d: str(item))

        print(dnew)
        print(dnew2)
        print(dnew3)
        print(dnew4)

        # {'child1': {'name': 'Emil', 'year': 16032}, 'child2': {'name': 'Tobias', 'year': 16056}, 'child3': {'name': 'Linus', 'year': 16088}}
        # {'child1': {'name': 'Emil', 'year': '2004'}, 'child2': {'name': 'Tobias', 'year': '2007'}, 'child3': {'name': 'Linus', 'year': '2011'}}
        # {1: {2: 122}, 2: {24: 566}} # sum can be applied to lists
        # {1: {2: '[4, 56, 2, 4, 56]'}, 2: {24: '[4, 56, 2, 4, 56, 444]'}} # the whole list is converted to a string
apply_function_dict_deep(d: dict, fu=lambda keys, item, d: item):
    Apply a given function to the values of a nested dictionary deeply and return a modified dictionary.

    Parameters:
    - d (dict): The input dictionary to be modified.
    - fu (function): The function to be applied to each value in the dictionary.
        - keys (list): The list of keys leading to the current value.
        - item (any): The current value in the dictionary.
        - d (dict): The input dictionary.

    Returns:
    - dict: A new dictionary with the same structure as the input, but with modified values.

    Example:
        from functionapplydict import apply_function_dict_deep
        child1 = {"name": "Emil", "year": 2004}
        child2 = {"name": "Tobias", "year": 2007}
        child3 = {"name": "Linus", "year": 2011}

        myfamily = {"child1": child1, "child2": child2, "child3": child3}


        def function1(keys, item, d):
            if isinstance(item, int):
                item = item * 8
            return item

        dnew = apply_function_dict_deep(d=myfamily, fu=function1)
        dnew2 = apply_function_dict_deep(d=myfamily, fu=lambda keys, item, d: str(item))

        ditest = {1: {2: [4, 56, 2, 4, 56]}, 2: {24: [4, 56, 2, 4, 56, 444]}}
        dnew3 = apply_function_dict_deep(d=ditest, fu=lambda keys, item, d: sum(item))
        dnew4 = apply_function_dict_deep(d=ditest, fu=lambda keys, item, d: str(item))

        print(dnew)
        print(dnew2)
        print(dnew3)
        print(dnew4)

        # {'child1': {'name': 'Emil', 'year': 16032}, 'child2': {'name': 'Tobias', 'year': 16056}, 'child3': {'name': 'Linus', 'year': 16088}}
        # {'child1': {'name': 'Emil', 'year': '2004'}, 'child2': {'name': 'Tobias', 'year': '2007'}, 'child3': {'name': 'Linus', 'year': '2011'}}
        # {1: {2: [4, 56, 2, 4, 56]}, 2: {24: [4, 56, 2, 4, 56, 444]}} # no change, because sum can't be applied to int
        # {1: {2: ['4', '56', '2', '4', '56']}, 2: {24: ['4', '56', '2', '4', '56', '444']}} # each int is converted to a string, but not the whole list

Project details


Release history Release notifications | RSS feed

This version

0.10

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

functionapplydict-0.10.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

functionapplydict-0.10-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file functionapplydict-0.10.tar.gz.

File metadata

  • Download URL: functionapplydict-0.10.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for functionapplydict-0.10.tar.gz
Algorithm Hash digest
SHA256 0efbc66184926ee73c9687bcca2ec192a0b1bb19ebf37d453dc712dafc1bb38d
MD5 52d8a56636e1170f860ccd329a495817
BLAKE2b-256 02e3fb60c527345842065a3d86da585762cec408273d332b1196b615a6c947c9

See more details on using hashes here.

File details

Details for the file functionapplydict-0.10-py3-none-any.whl.

File metadata

File hashes

Hashes for functionapplydict-0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 edca94fa3e512e1b9f4f7c89e1678dace09fbbc80e0d6a0eea6318a91cba4f36
MD5 2743bbece5112990f7db744e875b00f8
BLAKE2b-256 c320ecb9cfd8be56c9c0116bae1030bd5e51017d9098df1705da19f857beca35

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page