How to do List flattening inside the loop with list comprehension?
<p>here i am having list of dictionaries, my goal is to iterate over list and whenever there is 2 or more list available, i want to merge them and append in a output list, and whenever there is only one list it needs to be stored as it as.</p>

<pre><code>data = [
            [[{'font-weight': '1'},{'font-weight': '1'}],[{'font-weight': '2'},{'font-weight': '2'}]],
            [{'font-weight': '3'},{'font-weight': '3'},{'font-weight': '3'}],
            [[{'font-weight': '1'},{'font-weight': '1'}],[{'font-weight': '2'},{'font-weight': '2'}]],
            [{'font-weight': '3'},{'font-weight': '3'}]
        ]
</code></pre>

<p>I can do list flattening for particular element <code>data[0]</code></p>

<pre><code>print([item for sublist in data[0] for item in sublist])
[{'font-weight': '1'}, {'font-weight': '1'}, {'font-weight': '2'}, {'font-weight': '2'}]
</code></pre>

<p>Expected output :</p>

<pre><code>data = [
            [{'font-weight': '1'},{'font-weight': '1'},{'font-weight': '2'},{'font-weight': '2'}],
            [{'font-weight': '3'},{'font-weight': '3'},{'font-weight': '3'}],
            [{'font-weight': '1'},{'font-weight': '1'},{'font-weight': '2'},{'font-weight': '2'}]
            [{'font-weight': '3'},{'font-weight': '3'}]
        ]
</code></pre>

<p>iterate through list and check if each item is lists of list. If so flatten it.</p>

<hr>

<pre><code>data = [
         [[{'font-weight': '1'},{'font-weight': '1'}],[{'font-weight': '2'},{'font-weight': '2'}]],
         [{'font-weight': '3'},{'font-weight': '3'},{'font-weight': '3'}],
         [[{'font-weight': '1'},{'font-weight': '1'}],[{'font-weight': '2'},{'font-weight': '2'}]],
         [{'font-weight': '3'},{'font-weight': '3'}]
       ]
for n, each_item in enumerate(data):
   if any(isinstance(el, list) for el in each_item):
        data[n] = sum(each_item, [])

print data
</code></pre>

python list dictionary