LoginSignup
0
0

More than 1 year has passed since last update.

リスト内包表記

Posted at
py.qiita.py
l = []
for i in range(6):
    l.append(i)
print(l)

上で作っていたリストを1行にまとめられるのがリスト内包表記

py.qiita.py
#  上記のコードを一つにまとめられる
#  コードがスッキリするのと、処理が早いのがポイント
#  l = [追加する式、for文]この二つを書くこと
l = [ i for i in range(6)]
print(l) 

# if文を追加して条件をつけることができる
l2 = [i for i in range(1, 7) if i % 3 == 0]
print(l2)
0
0
1

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