2
1

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 5 years have passed since last update.

rangeからリストへの変換

Last updated at Posted at 2019-06-07

rangeで生成したものはイテラブルなオブジェクトだけどリストじゃない。

>>> range(3)
range(0, 3)

リストに変換するためによく見るのは以下。

>>> list(range(3))
[0, 1, 2]

以前、以下が等価なことを教えていただいた。

>>> A = [1,2,3]
>>> list(A)
[1, 2, 3]
>>> [*A]
[1, 2, 3]

つまりrange(イテレーター)をリストに変換するときも、

>>> [*range(3)]
[0, 1, 2]

と書ける。

可読性的にどうなんだろうと思うので、使いどきを選ぶかもしれない。

2
1
2

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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?