Is there a way to get multiple ngram orders using NTLK instead of obtaining a iterating over a generator?
<p>I need ngrams. I know <code>nltk.utils.ngrams</code> can be used to obtain ngrams, but in practice, the ngrams function returns a generator object. I can always iterate over it and store the ngrams in a list. But is there another, more direct way to obtaining these ngrams in a list without having to iterate over them?</p>

<p>or alternatively without <code>nltk</code>:</p>

<pre><code>from itertools import chain

def ngrams(L, n = 2):
    orders = [n] if type(n) is int else sorted(list(n))
    return list(chain(*[zip(*[L[i:] for i in range(n)]) for n in orders]))

&gt;&gt;&gt; ngrams([1,2,3,4,5], n = 3)
[(1, 2, 3), (2, 3, 4), (3, 4, 5)]
&gt;&gt;&gt; ngrams([1,2,3,4,5], n = [2,3])
[(1, 2), (2, 3), (3, 4), (4, 5), (1, 2, 3), (2, 3, 4), (3, 4, 5)]
</code></pre>

python nlp generator nltk n-gram