title: Python inserting multiple elements in one iteration in list comprehension conditionally
body: <p>Suppose we have to insert two elements in a list comprehension in one iteration, but we have to choose which two to insert. How do we make such a list comprehension. </p>
  
  <p>For example:- 
  Suppose we have a list <code>hostnames = ['aa', 'bb', 'dd', 'pp', 'bb', 'zz', 'hh']</code>.</p>
  
  <p>Now we want to modify this list such that hostnames starting with p should have an extra element called '_prd' ( eg. 'pp_prd') and similarly an extra element for hostnames starting with h with the value '_uat'.(eg 'hh_uat'). </p>
  
  <p>So the desired output for the above list would be <code>mod_hostnames = ['aa', 'bb', 'dd', 'pp_prd', 'pp', 'bb', 'zz', 'hh_uat','hh']</code>.</p>
  
  <p>Now I thought of writing it out like this -</p>
  
  <pre><code>&gt;&gt;&gt; fh=lambda x: x+'_uat'
  &gt;&gt;&gt; fp=lambda x:x+'_prd'
  &gt;&gt;&gt; fo=lambda x:x
  &gt;&gt;&gt; lis
  ['aa', 'bb', 'dd', 'pp', 'bb', 'zz', 'hh']
  &gt;&gt;&gt; hostnames = lis
  &gt;&gt;&gt; mod_hostnames = [f(host) for f in (fo,fp) if host[0]=='p' else f(host) for f in (fo,fh) if host[0]=='h' else host for host in hostnames]
    File "&lt;stdin&gt;", line 1
      mod_hostnames = [f(host) for f in (fo,fp) if host[0]=='p' else f(host) for f in (fo,fh) if host[0]=='h' else host for host in hostnames]
                                                                   ^
  SyntaxError: invalid syntax
  </code></pre>
  
  <p>We get a syntax error. I also know that in list comprehensions the second loop runs faster (just like in for loops) and in our code, the <code>host in hostnames</code> is running faster instead of the other desired way round. So I tried this :-</p>
  
  <pre><code>&gt;&gt;&gt; mod_hostnames = [f(host) for host in hostnames for f in (fo,fp) if host[0]=='p' else for f in (fo,fh) if host[0]=='h' else for f in (fo)]  
  File "&lt;stdin&gt;", line 1
      mod_hostnames = [f(host) for host in hostnames for f in (fo,fp) if host[0]=='p' else for f in (fo,fh) if host[0]=='h' else for f in (fo)]
                                                                                         ^
  SyntaxError: invalid syntax
  </code></pre>
  
  <p>Any suggestions? Or is this not possible in list comprehensions. <br/>I know that this is not readable at all and there are much better ways to write this out. (eg using a dict 'switch' to write this in one statement inside a for loop, or the good old if else in a loop, etc). </p>
  
  <p>Post solution edit: Received wonderful responses. Thanks! Can someone explain why the code I posted is wrong as well?<br/>
  I feel its because the statements evaluated when condition is true or false just resolve to an empty for loop: <code>for f in (fo,fp) if host[0]=='p' else for f in (fo,fh)</code> if host[0]=='p' the program goes to an empty for loop <code>for f in (fo,fp)</code>. Is this correct? <br/>
  A reverse engineering of my faulty comprehension into a for loop would clear this up I think.</p>