LoginSignup
0
0

More than 1 year has passed since last update.

Pythonでリストの要素を順に出力する簡潔な書き方を知った

Posted at

pythonでリストの要素を一つずつ出力したいとき、そのままprintするとリストの書式?が出力されてしまう。
イテラブルな要素の先頭に*を付けるとfor文を使うことなく、出力できることを知った。

lst = [1,2,3,4]

print(lst)  # => [1, 2, 3, 4]
print(*lst) # => 1 2 3 4

半角空白で区切りたくない場合はprintの引数にsep=''を加える。

print(*lst, sep='') # => 1234

for文を使わずコードが簡潔に書けて、かなり良いと思う。
なんで今まで知らなかったのだろう。

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