I need to assume for an empty sequence in this code for python
<p>The function prints the keyword length, then prints a sorted list of all the dictionary keywords of the required length, which have the highest frequency, followed by the frequency. For example, the following code:</p>

<pre><code> word_frequencies =  {"fish":9,  "parrot":8,  "frog":9,  "cat":9,  "stork":1,  "dog":4, "bat":9,  "rat":4}

 print_most_frequent(word_frequencies,3)
 print_most_frequent(word_frequencies,4)
 print_most_frequent(word_frequencies,5)
 print_most_frequent(word_frequencies,6)
 print_most_frequent(word_frequencies, 7) 
</code></pre>

<p>prints outs:</p>

<pre><code> 3 letter keywords: ['bat', 'cat'] 9
 4 letter keywords: ['fish', 'frog'] 9
 5 letter keywords: ['stork'] 1
 6 letter keywords: ['parrot'] 8
 7 letter keywords: [] 0
</code></pre>

<p>So far I have this code:</p>

<pre><code>def print_most_frequent(words_dict, word_len):
    right_length = {}
    for k, v in words_dict.items():
        if len(k) == word_len:
             right_length[k] = v
    max_freq = max(right_length.values())
    max_words = {}
    for k, v in right_length.items():
         if v == max_freq:
              max_words[k] = v
    print (str(word_len) + " letter keywords: " + str(sorted(max_words.keys())) + " " + str(max_freq))
</code></pre>

<p>and this gives me:</p>

<pre><code> 3 letter keywords: ['bat', 'cat'] 9
 4 letter keywords: ['fish', 'frog'] 9
 5 letter keywords: ['stork'] 1
 6 letter keywords: ['parrot'] 8
</code></pre>

<p>but not the empty sequence. How do I make an addition to my code so that it counts for the empty sequence.</p>

<pre><code> 7 letter keywords: [] 0
</code></pre>

<p>Simply add a fallback condition to the <code>max()</code> call so that it never tries to find the largest of an empty iterable:</p>

<pre><code>max_freq = max(right_length.values() or [0])
</code></pre>

<p>If <code>right_length.values()</code> is empty, <code>[0]</code> (which is not empty) will be used instead, and <code>max()</code> doesn't produce an error for that.</p>

python list function