Skip to main content

Converts a list of tuples to a nested "family tree dict"

pip install list2tree

Tested against Windows 10 / Python 3.10 / Anaconda

from list2tree import treedict,edit_value_in_treedict,get_value_in_treedict
import pprint as pa
pprintx=pa.PrettyPrinter(indent=1,width=1,compact=True)
pprint=pprintx.pprint
nameconnections = [
    ("Maria", "Anna"),
    ("Anna", "Joao"),
    ("Kasimir", "Maria"),
    ("Hans", "Fritz"),
    ("Fritz", "Anna"),
    ("Günther", "Wolfgang"),
    ("Joao", "Wolfgang"),
]
print("0) --------------------------------------------------------------")
mapped, airvar = treedict(
    pairs_list=nameconnections, main_mapping_keys=(), bi_rl_lr="lr"
)
pprint(mapped)
pprint(airvar)
print("1) --------------------------------------------------------------")
mapped, airvar = treedict(
    pairs_list=nameconnections, main_mapping_keys=(), bi_rl_lr="rl"
)
pprint(mapped)
pprint(airvar)
print("2) --------------------------------------------------------------")
mapped, airvar = treedict(
    pairs_list=nameconnections, main_mapping_keys=(), bi_rl_lr="bi"
)
pprint(mapped)
pprint(airvar)
print("3) --------------------------------------------------------------")
mapped, airvar = treedict(
    pairs_list=nameconnections, main_mapping_keys=("Anna", "Günther"), bi_rl_lr="lr"
)
pprint(mapped)
pprint(airvar)
print("4) --------------------------------------------------------------")
mapped, airvar = treedict(
    pairs_list=nameconnections, main_mapping_keys=("Anna", "Wolfgang"), bi_rl_lr="rl"
)
pprint(mapped)
pprint(airvar)
print("5) --------------------------------------------------------------")
mapped, airvar = treedict(
    pairs_list=nameconnections, main_mapping_keys=("Anna",), bi_rl_lr="bi"
)
pprint(mapped)
pprint(airvar)
print("6) --------------------------------------------------------------")
v1 = get_value_in_treedict(airvar, ("Anna", "Joao", "Wolfgang", "Günther"))
print(v1)
edit_value_in_treedict(
    airvar, ("Anna", "Joao", "Wolfgang", "Günther"), "new_value_edit"
)
pprint(airvar)
v2 = get_value_in_treedict(airvar, ("Anna", "Joao", "Wolfgang", "Günther"))
print(v2)
print("7) --------------------------------------------------------------")
0) --------------------------------------------------------------
[(1,
  ('Wolfgang',)),
 (2,
  ('Günther',
   'Wolfgang')),
 (3,
  ('Joao',
   'Wolfgang')),
 (4,
  ('Anna',
   'Joao',
   'Wolfgang')),
 (5,
  ('Fritz',
   'Anna',
   'Joao',
   'Wolfgang')),
 (6,
  ('Maria',
   'Anna',
   'Joao',
   'Wolfgang')),
 (7,
  ('Hans',
   'Fritz',
   'Anna',
   'Joao',
   'Wolfgang')),
 (8,
  ('Kasimir',
   'Maria',
   'Anna',
   'Joao',
   'Wolfgang'))]
{'Anna': {'Joao': {'Wolfgang': 4}},
 'Fritz': {'Anna': {'Joao': {'Wolfgang': 5}}},
 'Günther': {'Wolfgang': 2},
 'Hans': {'Fritz': {'Anna': {'Joao': {'Wolfgang': 7}}}},
 'Joao': {'Wolfgang': 3},
 'Kasimir': {'Maria': {'Anna': {'Joao': {'Wolfgang': 8}}}},
 'Maria': {'Anna': {'Joao': {'Wolfgang': 6}}},
 'Wolfgang': 1}
1) --------------------------------------------------------------
[(1,
  ('Günther',)),
 (2,
  ('Hans',)),
 (3,
  ('Kasimir',)),
 (4,
  ('Fritz',
   'Hans')),
 (5,
  ('Maria',
   'Kasimir')),
 (6,
  ('Wolfgang',
   'Günther')),
 (7,
  ('Wolfgang',
   'Joao',
   'Anna',
   'Maria',
   'Kasimir')),
 (8,
  ('Wolfgang',
   'Joao',
   'Anna',
   'Fritz',
   'Hans')),
 (9,
  ('Anna',
   'Maria',
   'Kasimir')),
 (10,
  ('Anna',
   'Fritz',
   'Hans')),
 (11,
  ('Joao',
   'Anna',
   'Maria',
   'Kasimir')),
 (12,
  ('Joao',
   'Anna',
   'Fritz',
   'Hans'))]
{'Anna': {'Fritz': {'Hans': 10},
          'Maria': {'Kasimir': 9}},
 'Fritz': {'Hans': 4},
 'Günther': 1,
 'Hans': 2,
 'Joao': {'Anna': {'Fritz': {'Hans': 12},
                   'Maria': {'Kasimir': 11}}},
 'Kasimir': 3,
 'Maria': {'Kasimir': 5},
 'Wolfgang': {'Günther': 6,
              'Joao': {'Anna': {'Fritz': {'Hans': 8},
                                'Maria': {'Kasimir': 7}}}}}
2) --------------------------------------------------------------
[(1,
  ('Anna',
   'Joao',
   'Wolfgang',
   'Günther')),
 (2,
  ('Anna',
   'Maria',
   'Kasimir')),
 (3,
  ('Anna',
   'Fritz',
   'Hans')),
 (4,
  ('Fritz',
   'Anna',
   'Joao',
   'Wolfgang',
   'Günther')),
 (5,
  ('Fritz',
   'Anna',
   'Maria',
   'Kasimir')),
 (6,
  ('Fritz',
   'Hans')),
 (7,
  ('Günther',
   'Wolfgang',
   'Joao',
   'Anna',
   'Maria',
   'Kasimir')),
 (8,
  ('Günther',
   'Wolfgang',
   'Joao',
   'Anna',
   'Fritz',
   'Hans')),
 (9,
  ('Hans',
   'Fritz',
   'Anna',
   'Joao',
   'Wolfgang',
   'Günther')),
 (10,
  ('Hans',
   'Fritz',
   'Anna',
   'Maria',
   'Kasimir')),
 (11,
  ('Joao',
   'Wolfgang',
   'Günther')),
 (12,
  ('Joao',
   'Anna',
   'Maria',
   'Kasimir')),
 (13,
  ('Joao',
   'Anna',
   'Fritz',
   'Hans')),
 (14,
  ('Kasimir',
   'Maria',
   'Anna',
   'Joao',
   'Wolfgang',
   'Günther')),
 (15,
  ('Kasimir',
   'Maria',
   'Anna',
   'Fritz',
   'Hans')),
 (16,
  ('Maria',
   'Anna',
   'Joao',
   'Wolfgang',
   'Günther')),
 (17,
  ('Maria',
   'Anna',
   'Fritz',
   'Hans')),
 (18,
  ('Maria',
   'Kasimir')),
 (19,
  ('Wolfgang',
   'Günther')),
 (20,
  ('Wolfgang',
   'Joao',
   'Anna',
   'Maria',
   'Kasimir')),
 (21,
  ('Wolfgang',
   'Joao',
   'Anna',
   'Fritz',
   'Hans'))]
{'Anna': {'Fritz': {'Hans': 3},
          'Joao': {'Wolfgang': {'Günther': 1}},
          'Maria': {'Kasimir': 2}},
 'Fritz': {'Anna': {'Joao': {'Wolfgang': {'Günther': 4}},
                    'Maria': {'Kasimir': 5}},
           'Hans': 6},
 'Günther': {'Wolfgang': {'Joao': {'Anna': {'Fritz': {'Hans': 8},
                                            'Maria': {'Kasimir': 7}}}}},
 'Hans': {'Fritz': {'Anna': {'Joao': {'Wolfgang': {'Günther': 9}},
                             'Maria': {'Kasimir': 10}}}},
 'Joao': {'Anna': {'Fritz': {'Hans': 13},
                   'Maria': {'Kasimir': 12}},
          'Wolfgang': {'Günther': 11}},
 'Kasimir': {'Maria': {'Anna': {'Fritz': {'Hans': 15},
                                'Joao': {'Wolfgang': {'Günther': 14}}}}},
 'Maria': {'Anna': {'Fritz': {'Hans': 17},
                    'Joao': {'Wolfgang': {'Günther': 16}}},
           'Kasimir': 18},
 'Wolfgang': {'Günther': 19,
              'Joao': {'Anna': {'Fritz': {'Hans': 21},
                                'Maria': {'Kasimir': 20}}}}}
3) --------------------------------------------------------------
[(1,
  ('Günther',
   'Wolfgang')),
 (2,
  ('Anna',
   'Joao',
   'Wolfgang'))]
{'Anna': {'Joao': {'Wolfgang': 2}},
 'Günther': {'Wolfgang': 1}}
4) --------------------------------------------------------------
[(1,
  ('Wolfgang',
   'Günther')),
 (2,
  ('Wolfgang',
   'Joao',
   'Anna',
   'Maria',
   'Kasimir')),
 (3,
  ('Wolfgang',
   'Joao',
   'Anna',
   'Fritz',
   'Hans')),
 (4,
  ('Anna',
   'Maria',
   'Kasimir')),
 (5,
  ('Anna',
   'Fritz',
   'Hans'))]
{'Anna': {'Fritz': {'Hans': 5},
          'Maria': {'Kasimir': 4}},
 'Wolfgang': {'Günther': 1,
              'Joao': {'Anna': {'Fritz': {'Hans': 3},
                                'Maria': {'Kasimir': 2}}}}}
5) --------------------------------------------------------------
[(1,
  ('Anna',
   'Joao',
   'Wolfgang',
   'Günther')),
 (2,
  ('Anna',
   'Maria',
   'Kasimir')),
 (3,
  ('Anna',
   'Fritz',
   'Hans'))]
{'Anna': {'Fritz': {'Hans': 3},
          'Joao': {'Wolfgang': {'Günther': 1}},
          'Maria': {'Kasimir': 2}}}
6) --------------------------------------------------------------
1
{'Anna': {'Fritz': {'Hans': 3},
          'Joao': {'Wolfgang': {'Günther': 'new_value_edit'}},
          'Maria': {'Kasimir': 2}}}
new_value_edit
7) --------------------------------------------------------------




treedict(
    pairs_list: list | tuple, main_mapping_keys: tuple | list = (), bi_rl_lr: str = "lr"
):
    """
    This function takes a list or tuple of pairs, a tuple or list of main mapping keys, and a string indicating the type
    of bidirectional mapping to be used. It returns a tuple of flattened dictionary and a nested dictionary.

    :param pairs_list: A list or tuple of pairs.
    :type pairs_list: list | tuple
    :param main_mapping_keys: A tuple or list of main mapping keys.
    :type main_mapping_keys: tuple | list
    :param bi_rl_lr: A string indicating the type of bidirectional mapping to be used. Default is "lr", Valid options are
                     lr - left to right / rl - right to left / bi - bidirectional
    :type bi_rl_lr: str
    :return: A tuple of flattened dictionary and a nested dictionary.
    :rtype: tuple
    """
	
edit_value_in_treedict(d: dict, keys: list | tuple, newvalue: Any) -> None:
    """
    Edit the value of a nested dictionary given a list of keys.

    Args:
        d (dict): The nested dictionary to be edited.
        keys (list,tuple): A list of keys representing the path to the value to be edited.
        newvalue (Any): The new value to be assigned to the specified key path.

    Returns:
        None: This function does not return anything, but it changes the ORIGINAL dict! If you want to keep a copy of
        the original dict, use copy.deepcopy(d) before you call the function

    Raises:
        KeyError: If any of the keys in the list do not exist in the dictionary.

    Example:
        d = {'a': {'b': {'c': 1}}}
        keys = ['a', 'b', 'c']
        newvalue = 2
        edit_value_in_treedict(d, keys, newvalue)
        # d is now {'a': {'b': {'c': 2}}}
    """	
	
	
get_value_in_treedict(d: dict, keys: list | tuple) -> Any:
    """
    Returns the value in a nested dictionary `d` at the specified `keys`.

    Args:
        d (dict): The nested dictionary to search for the value.
        keys (list|tuple): The list or tuple of keys to traverse the nested dictionary.

    Returns:
        Any: The value at the specified `keys` in the nested dictionary `d`.

    Raises:
        TypeError: If `d` is not a dictionary or `keys` is not a list or tuple.
        KeyError: If any of the keys in `keys` do not exist in the nested dictionary `d`.
    """
	

Metadata

Release files for list2tree 0.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for list2tree 0.10
File Size Uploaded Provenance Actions
list2tree-0.10.tar.gz 6.7 kB Without provenance Details for list2tree-0.10.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for list2tree 0.10
File Interpreter ABI Platform Actions
list2tree-0.10-py3-none-any.whl Python 3 none any Details for list2tree-0.10-py3-none-any.whl

Total release size: 16.2 kB

Release files / list2tree-0.10.tar.gz

Download URL list2tree-0.10.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
0eabfb47d77e96e24a8bfc444746db935aa550c08b85efd0996dc13606f11b6c
BLAKE2b-256 checksum
How to use checksums
58435bad03e51c36cd0a5140eff13c2c3595d3ad11bdff5ba7b4fe39b5e5c748
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.10

Release files / list2tree-0.10-py3-none-any.whl

Download URL list2tree-0.10-py3-none-any.whl
Size 9.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d820de0381e7cc18be168d3fa3d2cd2d0ee8aef1185c7c1f1e468d93685f90e7
BLAKE2b-256 checksum
How to use checksums
eea2c845e5b62b171bab1fbcc8ffd64f9acd6ae0fe8e26c1ecf67dbee7965d88
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.10

Security for list2tree 0.10

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.10 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page