Creating a dictionary where the key is an integer and the value is the length of a random sentence
<p>Super new to to python here, I've been struggling with this code for a while now. Basically the function returns a dictionary with the integers as keys and the values are all the words where the length of the word corresponds with each key.</p>

<p>So far I'm able to create a dictionary where the values are the total number of each word but not the actual words themselves.</p>

<p>So passing the following text</p>

<pre><code>"the faith that he had had had had an affect on his life"
</code></pre>

<p>to the function</p>

<pre><code>def get_word_len_dict(text):
    result_dict = {'1':0, '2':0, '3':0, '4':0, '5':0, '6' :0}
    for word in text.split():
        if str(len(word)) in result_dict:
            result_dict[str(len(word))] += 1
    return result_dict
</code></pre>

<p>returns</p>

<pre class="lang-none prettyprint-override"><code>1 - 0
2 - 3
3 - 6
4 - 2
5 - 1
6 - 1
</code></pre>

<p>Where I need the output to be:</p>

<pre class="lang-none prettyprint-override"><code>2 - ['an', 'he', 'on']
3 - ['had', 'his', 'the']
4 - ['life', 'that']
5 - ['faith']
6 - ['affect']
</code></pre>

<p>I think I need to have to return the values as a list. But I'm not sure how to approach it.</p>

<p>Your code is counting the occurrence of each word length - but not storing the words themselves.</p>

<p>In addition to capturing each word into a list of words with the same size, you also appear to want:</p>

<ol>
<li>If a word length is not represented, do not return an empty list for that length - just don't have a key for that length.</li>
<li>No duplicates in each word list</li>
<li>Each word list is sorted</li>
</ol>

<p>A set container is ideal for accumulating the words - sets naturally eliminate any duplicates added to them. </p>

<p>Using defaultdict(sets) will setup an empty dictionary of sets -- a dictionary key will only be created if it is referenced in our loop that examines each word.</p>

<pre><code>from collections import defaultdict 

def get_word_len_dict(text):

    #create empty dictionary of sets 
    d = defaultdict(set)

    # the key is the length of each word
    # The value is a growing set of words
    # sets automatically eliminate duplicates
    for word in text.split():
        d[len(word)].add(word)

    # the sets in the dictionary are unordered
    # so sort them into a new dictionary, which is returned
    # as a dictionary of lists

    return {i:sorted(d[i]) for i in d.keys()}
</code></pre>

<p>In your example string of </p>

<pre><code>a="the faith that he had had had had an affect on his life"
</code></pre>

<p>Calling the function like this:</p>

<pre><code>z=get_word_len_dict(a)
</code></pre>

<p>Returns the following list:</p>

<pre><code>print(z)
{2: ['an', 'he', 'on'], 3: ['had', 'his', 'the'], 4: ['life', 'that'], 5: ['faith'], 6: ['affect']}
</code></pre>

<p>The type of each value in the dictionary is "list".</p>

<pre><code>print(type(z[2]))
&lt;class 'list'&gt;
</code></pre>

python list python-3.x dictionary