in reply to Until loop / unique

Were you thinking of doing this recursively?

If so I'd consider a count of keys to drive the decision to dive any deeper. Carry the prefix with you as you recurse so you can assemble the full key path before returning it to the calling layer.