title: Double List Comprehension in Python?
body: <p>I have a function that takes a list of tables from a database instance and returns the table's column names.</p>
  <pre><code>def list_columns(self):
      tables = self.list_tables()
      col_list = []
      for t in tables:
          for column in tables[t].columns:
              col_list.append(column)
      return col_list
  </code></pre>
  <p>This will return what we're looking for, which is the names of the columns</p>
  <p>[Column('id', INTEGER(), table=, primary_key=True, nullable=False, server_default=DefaultClause(&lt;sqlalchemy.sql.elements.TextClause object at 0x7f2d9a7ded68&gt;, for_update=False)),</p>
  <p>When I try to place this same code into a list comprehension like so:</p>
  <pre><code>def list_columns(self):
      tables = self.list_tables()
      col_list = [column for t in tables for column in tables[t].columns]
      return col_list
  </code></pre>
  <p>The <code>col_list</code> returned is not the same as in the first function.</p>
  <p>When I rearrange the order of the list comprehension like this:</p>
  <pre><code>def list_columns(self):
      tables = self.list_tables()
      col_list = [column for column in tables[t].columns for t in tables]
      return col_list
  </code></pre>
  <p>There is an error on the second to last line <code>Unresolved reference 't'</code></p>
  <p>How can I write these two for loops in a list comprehension?</p>
  <p>EDITED - thanks for the input, a closer look revealed there is a different output that is shown when using print():
  There is something odd occurring with the print statement.</p>
  <pre><code>tables = self.list_tables()
  col_list = []
  for t in tables:
      for column in tables[t].columns:
          print(column)
  </code></pre>
  <p>This will print a list like:</p>
  <pre><code>image.id
  image.owner_id
  image.location
  </code></pre>
  <p>Which is what we're looking for.
  However, when I append <code>column</code> to a list and then print out that list, I'd expect it to be the same as what we see above.</p>
  <p>But this code:</p>
  <pre><code>        tables = self.list_tables()
          col_list = []
          for t in tables:
              for column in tables[t].columns:
                  col_list.append(column)
          print(col_list)
  </code></pre>
  <p>Prints out something like this:</p>
  <pre><code>[Column('id', INTEGER(), table=&lt;image&gt;, primary_key=True, nullable=False, server_default=DefaultClause(&lt;sqlalchemy.sql.elements.TextClause object at 0x7f12c60b1da0&gt;, for_update=False)), Column('owner_id', UUID(), table=&lt;image&gt;, nullable=False), ]
  </code></pre>