Having trouble finding the greatest length of whitespace in string
<p>I need to find the greatest length of white space in the string. No idea whats wrong. </p>

<pre><code>def check1(x):
    cc = ()
    for i in x:
        if i != " ":
            lis.append(cc)
            cc=0
        else:
            cc +=1
            new.append(cc)
            print(cc)
</code></pre>

<p>I'm not sure whats wrong, its not adding to the appending list. </p>

<p>You could use a simple <code>itertools.groupby</code>:</p>

<pre><code>&gt;&gt;&gt; from itertools import groupby
&gt;&gt;&gt; s = 'foo     bar       baz          lalala'
&gt;&gt;&gt; max(len(list(v)) for is_sp, v in groupby(s, str.isspace) if is_sp)
10
</code></pre>

<p>The groupby will find consecutive runs of whitespace (<code>is_sp == True</code>) and consecutive runs of non-whitespace (<code>is_sp == False</code>).  In our case, we only care about the runs of whitespace, so we filter the non-whitespace cases away and then get the length of the consecutive runs.  Finally, the only thing that is left is to pick the largest of all of lengths.</p>

python string