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

C# 使いの Python メモ

Last updated at Posted at 2021-09-21

FirstOrDefault

C#であればそれを返しなければ null にしたい LINQ の関数と言えば FirstOrDefault

employees.FirstOrDefault(p => p.name == "Tanaka)

Python でやるにはイテレータの next 関数が、次の要素がなければデフォルト値を返すように書けるので、こんなふうに書けば良いみたいです。

tanaka = next((e for e in employees if e.name == "Tanaka"), None)

参考:loekvandenouweland.com/content/first-or-default-in-python.html

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