import copy


def move_hosts_from_inventory_group_to_group(inventory, old_group, new_group):
  """Move the hosts from one inventory group to another, returning the new inventory dict (deep copy).
  
     The dict in the inventory parameter is not manipulated.
  """
  new_inventory = copy.deepcopy(inventory)
  old_group_hosts_dict = new_inventory['all']['children']['homek8s']['children'][old_group]['hosts']

  if new_inventory['all']['children']['homek8s']['children'][new_group]['hosts']:
    #add hosts from old group to new group
    new_inventory['all']['children']['homek8s']['children'][new_group]['hosts'].update(old_group_hosts_dict)
  else:
    #new group was empty, setting hosts dict directly
    new_inventory['all']['children']['homek8s']['children'][new_group]['hosts'] = old_group_hosts_dict

  new_inventory['all']['children']['homek8s']['children'][old_group]['hosts'] = {}
  return new_inventory