<!--
{
  "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/subscript(_:)-4bhoo",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Subscript",
  "symbol" : {
    "kind" : "Instance Subscript",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:SDyx3key_q_5valuetSD5IndexVyxq__Gcip"
  },
  "title" : "subscript(_:)"
}
-->

# subscript(_:)

Accesses the key-value pair at the specified position.

```
subscript(position: Dictionary<Key, Value>.Index) -> Dictionary<Key, Value>.Element { get }
```

## Parameters

`position`

The position of the key-value pair to access.
`position` must be a valid index of the dictionary and not equal to
`endIndex`.

## Return Value

A two-element tuple with the key and value corresponding to
`position`.

## Overview

This subscript takes an index into the dictionary, instead of a key, and
returns the corresponding key-value pair as a tuple. When performing
collection-based operations that return an index into a dictionary, use
this subscript with the resulting value.

For example, to find the key for a particular value in a dictionary, use
the `firstIndex(where:)` method.

```
let countryCodes = ["BR": "Brazil", "GH": "Ghana", "JP": "Japan"]
if let index = countryCodes.firstIndex(where: { $0.value == "Japan" }) {
    print(countryCodes[index])
    print("Japan's country code is '\(countryCodes[index].key)'.")
} else {
    print("Didn't find 'Japan' as a value in the dictionary.")
}
// Prints "(key: "JP", value: "Japan")"
// Prints "Japan's country code is 'JP'."
```

---

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)