LoginSignup
2
0

More than 3 years have passed since last update.

Pythonのスライスのstep機能

Last updated at Posted at 2019-07-24

スライスにはstep機能がある

Pythonのlistのスライスには指定した数ごとに値を取り出してくれるstep機能がある。

list[start:stop:step]
のように開始と終了のインデックスの後にstepの値を指定する。

以下例。

num_list = [1, 2, 3, 4, 5, 6, 7]
print(num_list[1:6])   #[2, 3, 4, 5, 6]
print(num_list[::2])   #[1, 3, 5, 7]
print(num_list[1::2])  #[2, 4, 6]
print(num_list[:6:2])  #[1, 3, 5]
print(num_list[1:6:2]) #[2, 4, 6]
2
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
2
0