Access key value pairs in gensim dictionary

3.8k Views Asked by At

I am working with the gensim dictionary. For example, you can print print(dictionary.token2id), as shown here https://radimrehurek.com/gensim/tut1.html. I can also print dictionary:

Dictionary(7 unique tokens: [u'nra', u'canon', u'deuterium', u'ion', u'facebook']...)

How do I access the key value pairs of the dictionary object, however?

1

There are 1 best solutions below

0
On BEST ANSWER

Token2id is a standard python dict. You can iterate like a standard dict:

Python 2:

for k, v in dictionary.token2id.iteritems():
    print k, v

For Python 3 use items():

for k, v in dictionary.token2id.items():
    print(k, v)