1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

PythonAdvent Calendar 2023

Day 16

Pythonで「リストの指定した位置へ要素を挿入する」の動作を確認してみた

Last updated at Posted at 2023-12-16

概要

Pythonで「リストの指定した位置へ要素を挿入する」の動作を確認してみました。以下のページを参考にしました。

実装

以下のファイルを作成しました。

sample.py
mylist = ["Blue", "Red", "Green"]
# "White" をインデックス 2 の前に挿入
mylist.insert(2, "White")
print(mylist)
# "Black" を先頭に挿入
mylist.insert(0, "Black")
print(mylist)
mylist = ["Isu", "Uma", "Ashika"]
addlist = ["Ushi", "Inu"]
# 別のリストをインデックス 1 の前に挿入
mylist[1:1] = addlist
print(mylist)

以下のコマンドを実行しました。

$ python3 sample.py 
['Blue', 'Red', 'White', 'Green']
['Black', 'Blue', 'Red', 'White', 'Green']
['Isu', 'Ushi', 'Inu', 'Uma', 'Ashika']

まとめ

何かの役に立てばと。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?