Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "word for word" syntax mean in Python?

Tags:

python

gensim

I see the following script snippet from the gensim tutorial page.

What's the syntax of word for word in below Python script?

>> texts = [[word for word in document.lower().split() if word not in stoplist]
>>          for document in documents]
like image 725
smwikipedia Avatar asked Apr 16 '26 20:04

smwikipedia


2 Answers

This is a list comprehension. The code you posted loops through every element in document.lower.split() and creates a new list that contains only the elements that meet the if condition. It does this for each document in documents.

Try it out...

elems = [1, 2, 3, 4]
squares = [e*e for e in elems]  # square each element
big = [e for e in elems if e > 2]  # keep elements bigger than 2

As you can see from your example, list comprehensions can be nested.

like image 152
ChrisP Avatar answered Apr 18 '26 09:04

ChrisP


That is a list comprehension. An easier example might be:

evens = [num for num in range(100) if num % 2 == 0]
like image 35
tckmn Avatar answered Apr 18 '26 09:04

tckmn