How do I check for an empty nested list in Python?
<p>Say I have a list like  </p>

<pre><code>cata = [["Shelf", 12, "furniture", [1,1]],
        ["Carpet", 50, "furnishing", []]]
</code></pre>

<p>and I want to find out in each nested list if it's empty or not.</p>

<p>I am aware of using for loops to iterate through the lists, and if statements to verify as I have in a function</p>

<pre><code>def myfunc(cata):
    for inner in cata:
        for inner_2 in inner[3]:
            if inner_2:
                return "True"
            else:
                return "False"
</code></pre>

<p>However instead of returning: </p>

<pre><code>'True'
'False'
</code></pre>

<p>All I get is:</p>

<pre><code>'False'
</code></pre>

<p>Is there some method of searching nested loops that I'm missing?</p>

<p>You're <code>return</code>ing from the function, that essentially means that you don't evaluate every element.</p>

<p>Apart from that <code>for inner_2 in inner[3]:</code> will not do what you want because it won't execute for empty lists (<em>it doesn't have elements to iterate though!</em>). What you could do is go through each list and <code>yield</code> the thuth-ness of <code>inner[3]</code>:</p>

<pre><code>def myfunc(cata):
    for inner in cata:
        yield bool(inner[3])
</code></pre>

<p><code>yield</code>ing will make the function a generator that can be iterated over and return your results:</p>

<pre><code>for i in myfunc(cata):
    print(i)
</code></pre>

<p>Prints:</p>

<pre><code>True
False
</code></pre>

<p>Of course, this can easily be transformed to a list comprehension that stores the results:</p>

<pre><code>r = [bool(x[3]) for x in cata]
</code></pre>

<p>With <code>r</code> now holding:</p>

<pre><code>[True, False]
</code></pre>

python list python-3.x if-statement for-loop