title: How to convert code to a list comprehension
body: <p>I'm trying to rewrite the code below as a list comprehension, but the line - `if groceries[k] == v and k not in most_needed' - does not work as desired. It generates an error msg that most_needed is not identified and when it's declared, the code produces duplicates when the values are the same - e.g. two pastas and two salsas. The output is a sorted list of items with the keys of the values printed in reverse order. Can someone advise how to correct? Thanks.</p>
  <pre><code>groceries = {'apples': 5, 'pasta': 3, 'carrots': 12, 'orange juice': 2, 'bananas': 8,   
              'popcorn': 1, 'salsa': 3, 'cereal': 4, 'coffee': 5, 'granola bars': 15,  
              'onions': 7, 'rice': 1, 'peanut butter': 2, 'spinach': 9}
  most_needed = []  
  
  for v in sorted(groceries.values(), reverse=True):
      for k in groceries:
          if groceries[k] == v and k not in most_needed:
              most_needed.append(k)
              break
  print(', '.join(most_needed) + '.')
  
  # prints --&gt; granola bars, carrots, spinach, bananas, onions, coffee, apples, cereal, 
    salsa, pasta, peanut butter, orange juice, rice, popcorn 
  
  most_needed = [k for v in sorted(groceries.values(), reverse=True) for k in groceries
                 if groceries[k] == v and k not in most_needed] 
  </code></pre>