0
2

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

【Python初心者】1つのリストを分割する(5行)。

Last updated at Posted at 2020-07-30

結論

これを覚えよう
split_list(l, n)

#l: リスト
#n: サブリストの要素数

使い方

リストを分断するよ
def split_list(l, n):
    for idx in range(0, len(l), n):
        yield l[idx:idx + n]
 
l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] #元のリスト
result = list(split_list(l, 3)) #いくつの要素ずつに分けるか

print(result) # [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?