# Replace ranges on OrderedDictionary

**URL:** <https://forums.swift.org/t/replace-ranges-on-ordereddictionary/62219>\
**Category:** Collections\
**Created:** [December 25, 2022, 9:14pm UTC](https://forums.swift.org/t/replace-ranges-on-ordereddictionary/62219 "2022-12-25T21:14:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![paeifbnaeufbpae](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/paeifbnaeufbpae/32/28102_2.png) [@paeifbnaeufbpae](https://forums.swift.org/u/paeifbnaeufbpae)\
**Post date:** [December 25, 2022, 9:14pm UTC](https://forums.swift.org/t/replace-ranges-on-ordereddictionary/62219/1 "2022-12-25T21:14:00Z")

</div>

Why is `OrderedDictionary` only a _Partial_ RangeReplaceableCollection? I'm looking to swap out my current `Array<(IDType, ValueType)>` for an `OrderedDictionary<IDType, ValueType>`, but rely on `replaceSubrange`, which is not implemented for `OrderedDictionary`. Wouldn't this method work with a dictionary literal as the `newValues` replacement argument?

---

<div class="post-metadata">

**Author:** ![pyrtsa](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/pyrtsa/32/740_2.png) [@pyrtsa](https://forums.swift.org/u/pyrtsa)\
**Post date:** [December 26, 2022, 9:16am UTC](https://forums.swift.org/t/replace-ranges-on-ordereddictionary/62219/2 "2022-12-26T09:16:14Z")

</div>

> [@paeifbnaeufbpae](#):
>
> Wouldn't this method work with a dictionary literal as the `newValues` replacement argument?

The `RandomAccessCollection` API as well as a subset of the `RangeReplaceableCollection` API can be found under the [`elements` computed property](https://github.com/apple/swift-collections/blob/main/Documentation/OrderedDictionary.md#sequence-and-collection-operations), to avoid confusion between element access by key and by index.

However, the uniqueness of keys isn't guaranteed in dictionary literals\[1\], and in case of `replaceSubrange`, it would not be enough for the literal to have unique keys; it should neither contain any of the keys existing outside the subrange being replaced.

* * *

1. Indeed the default dictionary literal type [`KeyValuePairs`](https://developer.apple.com/documentation/swift/keyvaluepairs) explicitly allows duplicate keys.

---

<div class="post-metadata">

**Author:** ![paeifbnaeufbpae](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/paeifbnaeufbpae/32/28102_2.png) [@paeifbnaeufbpae](https://forums.swift.org/u/paeifbnaeufbpae)\
**Post date:** [December 27, 2022, 8:55pm UTC](https://forums.swift.org/t/replace-ranges-on-ordereddictionary/62219/3 "2022-12-27T20:55:31Z")

</div>

Bottom line remains that there is no `replaceSubrange` equivalent. I think there should be one.

---

<div class="post-metadata">

**Author:** ![lorentey](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lorentey/32/1299_2.png) [@lorentey](https://forums.swift.org/u/lorentey)\
**Post date:** [December 28, 2022, 11:58pm UTC](https://forums.swift.org/t/replace-ranges-on-ordereddictionary/62219/4 "2022-12-28T23:58:52Z")

</div>

`OrderedDictionary` needs to ensure that its keys are unique, and this requirement is incompatible with arbitrary insertions and replacements.
