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>You say you want the keys to be integers but then you convert them to strings before storing them as a key. There is no need to do this in Python; integers can be dictionary keys.</p>

<p>Regarding your question, simply initialize the values of the keys to empty lists instead of the number 0. Then, in the loop, append the word to the list stored under the appropriate key (the length of the word), like this:</p>

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

def get_word_len_dict(text):
    result_dict = {i : [] for i in range(1, 7)}
    for word in text.split():
        length = len(word)
        if length in result_dict:
            result_dict[length].append(word)
    return result_dict      
</code></pre>

<p>This results in the following:</p>

<pre><code>&gt;&gt;&gt; get_word_len_dict(string)
{1: [], 2: ['he', 'an', 'on'], 3: ['the', 'had', 'had', 'had', 'had', 'his'], 4: ['that', 'life'], 5: ['faith'], 6: ['affect']}
</code></pre>

<p>If you, as you mentioned, wish to remove the duplicate words when collecting your input string, it seems elegant to use a set and convert to a list as a final processing step, if this is needed. Also note the use of <code>defaultdict</code> so you don't have to manually initialize the dictionary keys and values as a default value <code>set()</code> (i.e. the empty set) gets inserted for each key that we try to access but not others:</p>

<pre><code>from collections import defaultdict

string = "the faith that he had had had had an affect on his life"

def get_word_len_dict(text):
    result_dict = defaultdict(set)
    for word in text.split():
        length = len(word)
        result_dict[length].add(word)
    return {k : list(v) for k, v in result_dict.items()}
</code></pre>

<p>This gives the following output:</p>

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

python list python-3.x dictionary