I would like to do the following code, except in a neat 1-liner:
terms_set = set([])
for term in terms:
if index.has_key(term):
terms_set.add(index[term])
Is it possible to do? Must return a set.
You can use a set comprehension:
terms_set = {index[term] for term in terms if term in index}
Note that key in dict
is preferred over dict.has_key
, which was deprecated in Python 2 and is not available in Python 3.
terms_set = set(index[term] for term in terms if index.has_key(term))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With