while loop works without condition
<p>As far as I know, while statement requires condition to work, but here it works without any; how's that possible? How does <code>while q:</code> work?</p>

<p>The code is below:
...</p>

<pre><code>q = set([])
for i in range(N):
    q.add((i, 0))
    q.add((i, M - 1))
    w[i][0] = h[i][0]
    w[i][M - 1] = h[i][M - 1]
for i in range(M):
    q.add((0, i))
    q.add((N - 1, i))
    w[0][i] = h[0][i]
    w[N - 1][i] = h[N - 1][i]

while q:
    ci, cj = q.pop()
    for ii, jj in ((0, 1), (0, -1), (1, 0), (-1, 0)):
        ni, nj = ci + ii, cj + jj
        if 0 &lt;= ni &lt; N and 0 &lt;= nj &lt; M:
            if w[ni][nj] != h[ni][nj] and (w[ni][nj] is None or w[ni][nj] &gt; w[ci][cj]):
                w[ni][nj] = max(h[ni][nj], w[ci][cj])
                q.add((ni, nj))
</code></pre>

<p>There is a condition. 
The loop will iterate as long as there is an element in set q.</p>

<p>You would get a similar effect if you wrote:</p>

<pre><code>while len(q):
    # do something
</code></pre>

<p>or even</p>

<pre><code>while len(q) &gt; 0:
    #do something
</code></pre>

<p>However, these expressions could be viewed as perhaps a little redundant.</p>

python while-loop do-while