<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def flatten_phases_and_groups(phases_or_groups):
 """Recursively flatten nested lists for the list of phases or groups."""

  if isinstance(phases_or_groups, PhaseGroup):
    phases_or_groups = [phases_or_groups]
  ret = []
  for phase in phases_or_groups:
    if isinstance(phase, PhaseGroup):
      ret.append(phase.flatten())
    elif isinstance(phase, collections.Iterable):
      ret.extend(flatten_phases_and_groups(phase))
    else:
      ret.append(phase_descriptor.PhaseDescriptor.wrap_or_copy(phase))
  return ret