title: Using list comprehension. with nested if
body: <p>Given a list of filenames, we want to rename all the files with extension hpp to the extension h. To do this, we would like to generate a new list called newfilenames, consisting of the new filename. Using list comprehension#</p>
  <pre><code>filenames = [&quot;program.c&quot;, &quot;stdio.hpp&quot;, &quot;sample.hpp&quot;, &quot;a.out&quot;, &quot;math.hpp&quot;, &quot;hpp.out&quot;]
  newfilenames = [filename[:len(filename) - 3] + &quot;h&quot; for filename in filenames if filename.endswith(&quot;p&quot;)]
  print(newfilenames)
  </code></pre>
  <p>output came <code>[&quot;stdio.h&quot;, &quot;sample.h&quot;, &quot;math.h&quot;]</code>
  Should be <code>[&quot;program.c&quot;, &quot;stdio.h&quot;, &quot;sample.h&quot;, &quot;a.out&quot;, &quot;math.h&quot;, &quot;hpp.out&quot;]</code></p>