<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/Dictionary/init(uniqueKeysWithValues:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:SD20uniqueKeysWithValuesSDyxq_Gqd__n_tcSTRd__x_q_t7ElementRtd__lufc"
  },
  "title" : "init(uniqueKeysWithValues:)"
}
-->

# init(uniqueKeysWithValues:)

Creates a new dictionary from the key-value pairs in the given sequence.

```
init<S>(uniqueKeysWithValues keysAndValues: S) where S : Sequence, S.Element == (Key, Value)
```

## Parameters

`keysAndValues`

A sequence of key-value pairs to use for
the new dictionary. Every key in `keysAndValues` must be unique.

## Return Value

A new dictionary initialized with the elements of
`keysAndValues`.

## Discussion

You use this initializer to create a dictionary when you have a sequence
of key-value tuples with unique keys. Passing a sequence with duplicate
keys to this initializer results in a runtime error. If your
sequence might have duplicate keys, use the
`Dictionary(_:uniquingKeysWith:)` initializer instead.

The following example creates a new dictionary using an array of strings
as the keys and the integers in a countable range as the values:

```
let digitWords = ["one", "two", "three", "four", "five"]
let wordToValue = Dictionary(uniqueKeysWithValues: zip(digitWords, 1...5))
print(wordToValue["three"]!)
// Prints "3"
print(wordToValue)
// Prints "["three": 3, "four": 4, "five": 5, "one": 1, "two": 2]"
```

> Precondition: The sequence must not have duplicate keys.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)