0
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.

リストであそぼう

Last updated at Posted at 2022-02-25

リストの諸々

リストに突っ込む

a = "123abc"
l = list(a)
l
['1', '2', '3', 'a', 'b', 'c']

リストから文字列へ

print("".join(l))
123abc

一部を取り出す

スライス

l[1:3]
['2', '3']

条件を付ける

[x for x in l if x == "2"]
['2']

リストに要素を追加

一番後ろに追加 list.append()

l.append("d")
print(l)
['1', '2', '3', 'a', 'b', 'c', 'd']

指定したインデックスに追加 list.insert(index, 追加したい要素)

l.insert(3, 4)
print(l)
['1', '2', '3', 4, 'a', 'b', 'c', 'd']

リストの要素を削除する

すべて削除 list.clear()

a = "123abc"
l = list(a)
l.clear()
print(l)
[]

指定した位置の要素を削除し、削除した要素を取得 list.pop()

l = list("123abc")
print(l.pop(1))
print(l)
2
['1', '3', 'a', 'b', 'c']

指定した要素の最初の要素を削除 list.remove()

l = list("123abc123")
l.remove("1")
print(l)
['2', '3', 'a', 'b', 'c', '1', '2', '3']

指定した範囲を削除 del

l = list("123abc")
del l[2:4]
print(l)
['1', '2', 'b', 'c']
0
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
0
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?