title: Nested list comprehension with split() in python
body: <p>Long story short, I am trying to work with some crazy data I collected yesterday. A quick nested for loop would do this real easy but right now, I just want to get my nested list comprehension to work. There are a bunch of post on this subject and I am not sure whether it is just me or the because of Friday evening, I can't get it to work. My list comphrension is in format:</p>
  
  <pre><code>[ [str(j[0], sanitize(j[1]) for j in i.split(',')] for i in b]
  </code></pre>
  
  <p>where b is <code>['string, float\t\n', 'string, float\t\n']</code> or b might be a file object or whatever, doesn't matter a heck lot.</p>
  
  <p>I could simply do something like</p>
  
  <pre><code>for i in b:
      out.append(str(j[0]), sanitize(j[1])
  </code></pre>
  
  <p>But, it should be possible by list comprehension. IT SHOULD!!! And, somehow, I am not getting it right.</p>
  
  <p>Now, on to what I have done so far...</p>
  
  <pre><code>&gt;&gt;&gt; b
  ['08:54:55, 0.031250\n', '08:55:57, 0.031250\n']
  
  &gt;&gt;&gt; [i.split(',') for i in b]
  [['08:54:55', ' 0.031250\n'], ['08:55:57', ' 0.031250\n']]
  </code></pre>
  
  <p>What I want:</p>
  
  <pre><code>[['08:54:55', 0.031250], ['08:55:57', 0.031250]]
  </code></pre>
  
  <p>A multiple list comprehension should do the trick:</p>
  
  <p>Something like:</p>
  
  <pre><code>[ [j[0], j[1]] for j in i.split(',') for i in b] # the cleaning/str/float would be something like: str(j[0]), float(j[1])
  </code></pre>
  
  <p>BUT, this is what I get.</p>
  
  <pre><code>&gt;&gt;&gt; [ [j[0], j[1]] for j in i.split(',') for i in b]
  [['0', '8'], ['0', '8'], [' ', '0'], [' ', '0']]
  &gt;&gt;&gt; 
  &gt;&gt;&gt; [ [j[1]] for j in i.split(',') for i in b]
  [['8'], ['8'], ['0'], ['0']]
  </code></pre>
  
  <p>Any attempt to work on j[0] or j[1] does not pan out.</p>
  
  <p>To figure out what was going on, I did this:</p>
  
  <pre><code>&gt;&gt;&gt; [ [ pprint(type(j)) for j in i.split(',')] for i in b]
  &lt;type 'str'&gt;
  &lt;type 'str'&gt;
  &lt;type 'str'&gt;
  &lt;type 'str'&gt;
  [[None, None], [None, None]]
  </code></pre>
  
  <p>If I don't mess with <code>j[0]</code> or <code>j[1]</code>,</p>
  
  <pre><code>&gt;&gt;&gt; [ [ j for j in i.split(',')] for i in b]
  [['08:54:55', ' 0.031250\n'], ['08:55:57', ' 0.031250\n']]
  </code></pre>
  
  <p>The example with <code>pprint(type(j))</code> means even if I were to write an external function, it would not do the trick. I would simply be getting an empty list back from the list comprehension.</p>
  
  <p>What am I doing wrong? A lambda might do the trick?</p>