1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Python】毎週勉強するシリーズ ~リストの内包表記~

Last updated at Posted at 2025-03-07

概要

AI が流行ってる昨今 Python は必須級のスキルになってきてます。
時代に取り残されないようにコツコツと Python を勉強しようと思います!
三日坊主にならないように Qita でアウトプットしながら進めます💪

今日やること

とほほのPython入門 - リスト・タプル・辞書 のリストの内包表記を理解する。

リストの内包表記

リストの内包表記を用いることで、map(), filter(), lambda を使用しないで簡単なリスト演算を行うことができます。

1. 構文

リストの内包表記は [処理 for 変数名 in イテラブルオブジェクト] が基本的な構文です。

2. 説明

リストの内包表記を用いることで、map(), filter(), lambda を使用しないで簡単なリスト演算を行うことができます。

3. map() と比較してみる

a = [1, 2, 3]
print(list(map(int, a)))    #=> [1, 2, 3]
print([x for x in a])       #=> [1, 2, 3]   

4. filter() と比較してみる

a = [1, 2, 3]
print(list(filter(lambda x: x % 2 == 0, a)))    #=> [2]
print([x for x in a if x % 2 == 0])             #=> [2]   

感想

今日はリストの内包表記をやってみました!

今回も見ていただきありがとうございました。
また次回お会いしましょう!!

次回やること

とほほのPython入門 - 演算子 を理解する。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?