Count sub_lists length in python3
<p>My python is 3 ver.
I have a list, which contains sub lists, like:</p>

<pre><code>L=[[1,2], [3,4], [5,6,7]]
</code></pre>

<p>I want to calculate length of all sub_list and write down them to a dictionary, like this:</p>

<pre><code>D={2:2,3:1}
</code></pre>

<p>that is, we have 2 lists which have length 2, and 1 list which have the length 3.</p>

<p>It can be done by for-loop + check D[key] is exist or not.</p>

<p>May be there is some 'pythonic way?'</p>

<p>You need to iterate over the list, and check that the length exists as key or not. If it exists, increment the value by 1, else add the value as 1. Below is the sample code.</p>

<pre><code>&gt;&gt;&gt; L = [[1,2], [3,4], [5,6,7]]
&gt;&gt;&gt; my_len_dict = {}
&gt;&gt;&gt; for item in L:
...     item_length = len(item)
...     if item_length in my_len_dict:
...         my_len_dict[item_length] += 1
...     else:
...         my_len_dict[item_length] = 1
...
&gt;&gt;&gt; my_len_dict
{2: 2, 3: 1}
</code></pre>

python python-3.x