<!--
{
  "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/Array/insert(_:at:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:Sa6insert_2atyxn_SitF"
  },
  "title" : "insert(_:at:)"
}
-->

# insert(_:at:)

Inserts a new element at the specified position.

```
mutating func insert(_ newElement: Element, at i: Int)
```

## Parameters

`newElement`

The new element to insert into the array.

`i`

The position at which to insert the new element.
`index` must be a valid index of the array or equal to its `endIndex`
property.

## Discussion

The new element is inserted before the element currently at the specified
index. If you pass the array’s `endIndex` property as the `index`
parameter, the new element is appended to the array.

```
var numbers = [1, 2, 3, 4, 5]
numbers.insert(100, at: 3)
numbers.insert(200, at: numbers.endIndex)

print(numbers)
// Prints "[1, 2, 3, 100, 4, 5, 200]"
```

> Complexity: O(*n*), where *n* is the length of the array. If
> `i == endIndex`, this method is equivalent to `append(_:)`.

---

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)