from typing import Dict
from typing import Any
from typing import List
from typing import Tuple


def keylist_access_nested_dict(
		d : Dict[str,Any], 
		keys : List[str],
	) -> Tuple[dict,str]:
	"""given a keylist `keys`, return (x,y) where x[y] is d[keys]
	by pretending that `d` can be accessed dotlist-style, with keys in the list being keys to successive nested dicts, we can provide both read and write access to the element of `d` pointed to by `keys`
	
	### Parameters:
	 - `d : Dict[str,Any]`
	   dict to access
	 - `keys : List[str]`   
	   list of keys to nested dict `d`
	
	### Returns:
	 - `Tuple[dict,str]` 
	   dict is the final layer dict which contains the element pointed to by `keys`, and the string is the last key in `keys`
	"""
	
	fin_dict : dict = d
	for k in keys[:-1]:
		if k in fin_dict:
			fin_dict = fin_dict[k]
		else:
			fin_dict[k] = {}
			fin_dict = fin_dict[k]
	fin_key = keys[-1]

	return (fin_dict,fin_key)