One thing, though - traverse through the List<KeyValue> in reverse order, otherwise the result.remove(idx) will cause problems (potentially skipping an element)