Given a nested dictionary from k1 -> k2 > value
    transpose its outer and inner keys so it maps
    k2 -> k1 -> value.
555514
def transpose_nested_dictionary(nested_dict):
    
    result = defaultdict(dict)
    for k1, d in nested_dict.items():
        for k2, v in d.items():
            result[k2][k1] = v
    return result
437784
def expand_dict_as_keys(d):
    
    to_product = []
    for key, values in sorted(d.items()):
        # if we sort the inputs here, itertools.product will keep a stable sort order for us later
        key_values = sorted([(key, v) for v in utils.ensure_listable(values) if v is not None])
        if key_values:
            to_product.append(key_values)
    return list(itertools.product(*to_product))
598065
def fromkeys (cls, iterable, value=None):
        """"""
        d = cls()
        for k in iterable:
            dict.__setitem__(d, k.lower(), value)
        return d
1262107
def map_keys(pvs, keys):
    
    rs = []
    for pv in pvs:
        r = dict((v, None) for k, v in keys.items())
        for k, v in pv.items():
            if k in keys:
                r[keys[k]] = v
            r[k] = v
        rs.append(r)
    return rs
1357528
def _expand_one_key_dictionary(_dict):
    '''
    
    '''
    key = next(six.iterkeys(_dict))
    value = _dict[key]
    return key, value
965852
def flatten_nested_keys(body)
      options = {}
      body.each do |key, value|
        next unless value.is_a?(Hash)
        value = flatten_nested_keys(value.dup)
        value.each do |nested_key, nested_value|
          options["#{key}/#{nested_key}"] = nested_value
        end
        body.delete(key)
      end
      body.merge(options)
    end
383448
def dicts_equal(d1, d2):
  

  # check for different sizes
  if len(d1) != len(d2):
    return False
  # check for different keys
  for k in d1:
    if k not in d2:
      return False
  for k in d2:
    if k not in d1:
      return False

  # compare each element in dict
  for k in d1:
    if type(d1[k]) != type(d2[k]):
      # different value types
      return False

    # lists
    elif isinstance(d1[k], list):
      if not (sorted(d1[k]) == sorted(d2[k])):
        return False

    # nested dicts
    elif isinstance(d1[k], dict):
      if not dicts_equal(d1[k], d2[k]):
        return False

    # primitives
    else:
      if d1[k] != d2[k]:
        return False

  return True
376554
def merge_dictionaries(a, b):
  """"""
  res = {}
  for k in a:
    res[k] = a[k]
  for k in b:
    res[k] = b[k]
  return res
1173837
def _deep_merge_dict(a, b):
    """"""
    for k, v in b.items():
        if k in a and isinstance(a[k], dict) and isinstance(v, dict):
            _deep_merge_dict(a[k], v)
        else:
            a[k] = v
1283840
def merge(a, b):
    ""
    for key in b:
        if isinstance(a.get(key), dict) and isinstance(b.get(key), dict):
            merge(a[key], b[key])
        else:
            a[key] = b[key]
    return a
840801
def invertDictMapping(d):
    
    inv_map = {}
    for k, v in d.items():
        inv_map[v] = inv_map.get(v, [])
        inv_map[v].append(k)
    return inv_map
925141
def transpose_list(list_of_dicts):
    
    res = {}
    for d in list_of_dicts:
        for k, v in d.items():
            if k in res:
                res[k].append(v)
            else:
                res[k] = [v]
    return res
1028599
def convert_to_nested_dict(dotted_dict):
    """"""
    nested_dict = {}
    for k, v in iterate_flattened(dotted_dict):
        set_by_dotted_path(nested_dict, k, v)
    return nested_dict
1354584
def set_nested(self, klist, value):
        """"""
        keys = list(klist)
        if len(keys) > 0:
            curr_dict = self
            last_key = keys.pop()
            for key in keys:
                if not curr_dict.has_key(key) or not isinstance(curr_dict[key], 
                                                                NestedDict):
                    curr_dict[key] = type(self)()
                curr_dict = curr_dict[key]
            curr_dict[last_key] = value
405847
def recursive_asdict(d):
    """"""
    out = {}
    for k, v in asdict(d).iteritems():
        if hasattr(v, '__keylist__'):
            out[k] = recursive_asdict(v)
        elif isinstance(v, list):
            out[k] = []
            for item in v:
                if hasattr(item, '__keylist__'):
                    out[k].append(recursive_asdict(item))
                else:
                    out[k].append(item)
        else:
            out[k] = v
    return out
249075
def _dict_value_to_pairs(d):
    
    d = d[tz.first(d)]

    for k, v in d.items():
        yield {k: v}
1162397
def _update_intersection(dict1,dict2,**kwargs):
    '''
        
    '''
    if('deepcopy' in kwargs):
        deepcopy = kwargs['deepcopy']
    else:
        deepcopy = 1
    if(deepcopy == 1):
        dict1 = copy.deepcopy(dict1)
    else:
        pass
    for key in dict2:
        if(key in dict1):
            dict1[key] = dict2[key]
    return(dict1)