0

I am trying to extract first element from a dictionary built as follows:

vocab=vectorizer.vocabulary_
{k: v for k, v in sorted(vocab.items(), key=lambda item: item[1],reverse=True)}

Output:

{'zum': 887,
 'yet': 886,
 'written': 885,
 'write': 884,
 'worlds': 883,
 'world': 882}

I tried next(iter(vocab)), but it extracts only the first item. I would need the whole list as I want to build a new pandas dataframe with this list.

Desired output:

['zum',
 'yet',
 'written',
 'write',
 'worlds',
 'world']

1 Answers1

0

You can use the following:

vocab.keys()

example:

>>> d = {'zum': 887,
...  'yet': 886,
...  'written': 885,
...  'write': 884,
...  'worlds': 883,
...  'world': 882}
>>> d.keys()
['worlds', 'write', 'written', 'world', 'yet', 'zum']
akazuko
  • 1,394
  • 11
  • 19