---
title: "subscript(_:)"
framework: swift
role: symbol
role_heading: Instance Subscript
path: "swift/dictionary/subscript(_:)-4bhoo"
---

# subscript(_:)

Accesses the key-value pair at the specified position.

## Declaration

```swift
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

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

## Overview

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'."

## See Also

### Accessing Keys and Values

- [subscript(_:)](swift/dictionary/subscript(_:)-8rfql.md)
- [subscript(_:default:)](swift/dictionary/subscript(_:default:).md)
- [index(forKey:)](swift/dictionary/index(forkey:).md)
- [keys](swift/dictionary/keys-swift.property.md)
- [values](swift/dictionary/values-swift.property.md)
- [first](swift/dictionary/first.md)
- [randomElement()](swift/dictionary/randomelement().md)
- [randomElement(using:)](swift/dictionary/randomelement(using:).md)
