配列の指定した位置に値を挿入する Array.insertAt

配列公式データ取得日:2026年11月13日Python:Array.insert_at動作確認待ち

ひとことで言うと

指定された位置に値を挿入し、長さを 1 つ増やします。

ブロックとコード

ブロックの文言は公式の日本語版に合わせています(形は簡易表示です)。

引数(ひきすう)

引数とは、ブロックの中で自分で選んだり入力したりする部分のことです。

名前選べる値意味
index数値値を挿入するリスト中の位置を 0 から始まる値で指定します。たとえば:0
valueT

使用例

The size of the array grows by one. The element is added to the array at the position you want. If there's an element already at that position, then it and all the other elements after it are moved (shifted) to the next higher position. So, an array that has the numbers 4, 5, 9, 3 will be 4, 5, 9, 2, 3 if 2 is inserted at position 3. It looks like this in blocks:

JavaScript

let myNumbers = [4, 5, 9, 3];
myNumbers.insertAt(3, 2);

Python

myNumbers = [4, 5, 9, 3]
myNumbers.insert_at(3, 2)

Make a ordered array that has the numbers from a jumbled an array in order of lowest to highest.

JavaScript

let removed = 0
let jumbled = [4, 5, 2, 1, 6, 9, 0, 3, 8, 7]
let ordered = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
for (let item of jumbled) {
    ordered.insertAt(item, item)
    removed = ordered.removeAt(item + 1)
}

Python

removed = 0
jumbled = [4, 5, 2, 1, 6, 9, 0, 3, 8, 7]
ordered = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
for item in jumbled:
    ordered.insert_at(item, item)
    removed = ordered.remove_at(item + 1)
執筆:鈴木孝昌官公庁のWeb・システム開発を担当する現役プロジェクトマネージャー。マイクラカップ沖縄代表チームを指導(第6回・第7回、第7回TBS賞)。
沖縄マイクラ部で、子どもたちにMakeCodeを教えています。プロフィールと編集方針
Instagramで教室の様子を見る
noteで記事を読む