0

I'm working through a module on NLP, and they use this syntax to "create a dictionary that maps us from a word to its index in the word embeddings matrix."

index = {word: i for i, word in enumerate(english_words)}

I've never seen a loop that uses i for i before. I believe I understand what it's doing (taking every word at index i, and mapping it to the corresponding word in english_words. But, how does this syntax actually work? Do other languages use this syntax, or is this python-specific?

jizhihaoSAMA
  • 12,336
  • 9
  • 27
  • 49
jbcd13
  • 145
  • 8
  • 3
    Note that `i for i` isn't a fundamental element of this expression. The basic structure of a dictcomp is `{KEY: VALUE for VARIABLE(s) in ITERABLE}`. So `i` alone isn't the output (`word: i` is), and you are looping over both `i` and `word` together (since `enumerate()` produces 2-element tuples). – jasonharper May 11 '20 at 13:43
  • 1
    If it helps, you could also write it as `{word: i for (i, word) in enumerate(english_words)}` since `i, word` is a tuple. – wjandrea May 11 '20 at 14:05

2 Answers2

2

This is an example of dictionary comprehension.

What it is actually doing is:

For every word in english_words, separate the key (word) and value (i), and create an entry in the dictionary index of the form word: i

Equivalent to:

index = {}
counter = 0
for word in english_words:
   index[word] = counter
   counter += 1
wjandrea
  • 28,235
  • 9
  • 60
  • 81
0

It is called list comprehension. List comprehension is an elegant way to define and create lists based on existing lists. You can find out more about list comprehension here.

Other is enumerate(), this just unpacks the list. Returns index and value for the list. Same as .items() gives for dicts. Will get a better idea about this here