<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<oembed>
  <author_name>amano41</author_name>
  <author_url>https://blog.hatena.ne.jp/amano41/</author_url>
  <blog_title>知に至る病</blog_title>
  <blog_url>https://amano41.hatenablog.jp/</blog_url>
  <categories>
    <anon>Python</anon>
  </categories>
  <description>内包表記の中で場合分けしたい Python の内包表記には if 節をつけることができます。 &gt;&gt;&gt; [x for x in range(10) if x % 2 == 0] [0, 2, 4, 6, 8] しかし，これでできるのはデータのフィルタリングであって，else 節をつけて条件に応じて値を変えるということはできません。 条件に合致しないデータはすべて破棄されてしまいます。 試しに if 節の後に else 節をくっつけてみると Syntax Error になります。 &gt;&gt;&gt; [x for x in range(10) if x % 2 == 0 else -x] File &quot;&lt;std…</description>
  <height>190</height>
  <html>&lt;iframe src=&quot;https://hatenablog-parts.com/embed?url=https%3A%2F%2Famano41.hatenablog.jp%2Fentry%2Fpython-if-else-in-list-comprehension&quot; title=&quot;Python の内包表記で if ～ else を使った場合分けをする - 知に至る病&quot; class=&quot;embed-card embed-blogcard&quot; scrolling=&quot;no&quot; frameborder=&quot;0&quot; style=&quot;display: block; width: 100%; height: 190px; max-width: 500px; margin: 10px 0px;&quot;&gt;&lt;/iframe&gt;</html>
  <image_url></image_url>
  <provider_name>Hatena Blog</provider_name>
  <provider_url>https://hatena.blog</provider_url>
  <published>2016-10-26 00:09:04</published>
  <title>Python の内包表記で if ～ else を使った場合分けをする</title>
  <type>rich</type>
  <url>https://amano41.hatenablog.jp/entry/python-if-else-in-list-comprehension</url>
  <version>1.0</version>
  <width>100%</width>
</oembed>
