diff --git a/AUTHORS.md b/AUTHORS.md
index 6a233f99b..dcf297261 100644
--- a/AUTHORS.md
+++ b/AUTHORS.md
@@ -263,6 +263,8 @@
 - Bonifacio de Oliveira <https://github.com/Bonifacio2>
 - Vassilis Palassopoulos <https://github.com/palasso>
 - Ram Rachum <https://github.com/cool-RR>
+- Denali Molitor <https://github.com/dmmolitor>
+- Jacob Moorman <https://github.com/jdmoorman>
 
 ## Others whose work we've taken and included in NLTK, but who didn't directly contribute it:
 ### Contributors to the Porter Stemmer
diff --git a/nltk/lm/__init__.py b/nltk/lm/__init__.py
index 5b87ef7f1..2baa90bca 100644
--- a/nltk/lm/__init__.py
+++ b/nltk/lm/__init__.py
@@ -73,15 +73,7 @@ While not the most efficient, it is conceptually simple.
     >>> from nltk.util import everygrams
     >>> padded_bigrams = list(pad_both_ends(text[0], n=2))
     >>> list(everygrams(padded_bigrams, max_len=2))
-    [('<s>',),
-     ('a',),
-     ('b',),
-     ('c',),
-     ('</s>',),
-     ('<s>', 'a'),
-     ('a', 'b'),
-     ('b', 'c'),
-     ('c', '</s>')]
+    [('<s>',), ('<s>', 'a'), ('a',), ('a', 'b'), ('b',), ('b', 'c'), ('c',), ('c', '</s>'), ('</s>',)]
 
 We are almost ready to start counting ngrams, just one more step left.
 During training and evaluation our model will rely on a vocabulary that
diff --git a/nltk/util.py b/nltk/util.py
index 999625aa7..3d79c67e5 100644
--- a/nltk/util.py
+++ b/nltk/util.py
@@ -471,14 +471,7 @@ def pad_sequence(
 # add a flag to pad the sequence so we get peripheral ngrams?
 
 
-def ngrams(
-    sequence,
-    n,
-    pad_left=False,
-    pad_right=False,
-    left_pad_symbol=None,
-    right_pad_symbol=None,
-):
+def ngrams(sequence, n, **kwargs):
     """
     Return the ngrams generated from a sequence of items, as an iterator.
     For example:
@@ -514,24 +507,16 @@ def ngrams(
     :type right_pad_symbol: any
     :rtype: sequence or iter
     """
-    sequence = pad_sequence(
-        sequence, n, pad_left, pad_right, left_pad_symbol, right_pad_symbol
-    )
+    sequence = pad_sequence(sequence, n, **kwargs)
+    
+    # Creates the sliding window, of n no. of items.
+    # `iterables` is a tuple of iterables where each iterable is a window of n items.
+    iterables = tee(sequence, n)
 
-    history = []
-    while n > 1:
-        # PEP 479, prevent RuntimeError from being raised when StopIteration bubbles out of generator
-        try:
-            next_item = next(sequence)
-        except StopIteration:
-            # no more data, terminate the generator
-            return
-        history.append(next_item)
-        n -= 1
-    for item in sequence:
-        history.append(item)
-        yield tuple(history)
-        del history[0]
+    for i, sub_iterable in enumerate(iterables): # For each window,
+        for _ in range(i):                       # iterate through every order of ngrams
+            next(sub_iterable, None)             # generate the ngrams within the window.
+    return zip(*iterables) # Unpack and flattens the iterables.
 
 
 def bigrams(sequence, **kwargs):
@@ -574,30 +559,64 @@ def trigrams(sequence, **kwargs):
         yield item
 
 
-def everygrams(sequence, min_len=1, max_len=-1, **kwargs):
+def everygrams(sequence, min_len=1, max_len=-1, pad_left=False, pad_right=False, **kwargs):
     """
     Returns all possible ngrams generated from a sequence of items, as an iterator.
 
         >>> sent = 'a b c'.split()
+
+    New version outputs for everygrams.
         >>> list(everygrams(sent))
+        [('a',), ('a', 'b'), ('a', 'b', 'c'), ('b',), ('b', 'c'), ('c',)]
+
+    Old version outputs for everygrams.
+        >>> sorted(everygrams(sent), key=len)
         [('a',), ('b',), ('c',), ('a', 'b'), ('b', 'c'), ('a', 'b', 'c')]
+
         >>> list(everygrams(sent, max_len=2))
-        [('a',), ('b',), ('c',), ('a', 'b'), ('b', 'c')]
+        [('a',), ('a', 'b'), ('b',), ('b', 'c'), ('c',)]
 
-    :param sequence: the source data to be converted into trigrams
+    :param sequence: the source data to be converted into ngrams. If max_len is
+        not provided, this sequence will be loaded into memory
     :type sequence: sequence or iter
     :param min_len: minimum length of the ngrams, aka. n-gram order/degree of ngram
     :type  min_len: int
     :param max_len: maximum length of the ngrams (set to length of sequence by default)
     :type  max_len: int
+    :param pad_left: whether the ngrams should be left-padded
+    :type pad_left: bool
+    :param pad_right: whether the ngrams should be right-padded
+    :type pad_right: bool
     :rtype: iter(tuple)
     """
 
+    # Get max_len for padding.
     if max_len == -1:
-        max_len = len(sequence)
-    for n in range(min_len, max_len + 1):
-        for ng in ngrams(sequence, n, **kwargs):
-            yield ng
+        try:
+            max_len = len(sequence)
+        except TypeError:
+            sequence = list(sequence)
+            max_len = len(sequence)
+
+    # Pad if indicated using max_len.
+    sequence = pad_sequence(sequence, max_len, pad_left, pad_right, **kwargs)
+
+    # Sliding window to store grams.
+    history = list(islice(sequence, max_len))
+
+    # Yield ngrams from sequence.
+    while history:
+        for ngram_len in range(min_len, len(history)+1):
+            yield tuple(history[:ngram_len])
+
+        # Append element to history if sequence has more items.
+        try:
+            history.append(next(sequence))
+        except StopIteration:
+            pass
+
+        del history[0]
+
 
 
 def skipgrams(sequence, n, k, **kwargs):

nltk.everygrams() does not work with non-reusable iterables and iterables without lengths.
`nltk.util.everygrams()` ([code](https://github.com/nltk/nltk/blob/develop/nltk/util.py#L577-L600), [docs](https://nltk.readthedocs.io/en/latest/api/nltk.html?highlight=everygrams#nltk.util.everygrams)) claims to support both sequences and iterables. However, for iterables that are not reusable or iterables without a length function, everygrams either raises an exception or behaves incorrectly. In particular:
* When calling `everygrams(obj)` without its optional `max_len` argument, it attempts to take the length of its first argument, which may not support `len(obj)` resulting in a `TypeError`.
* When calling `everygrams(obj, max_len=2)` for example, if obj is an iterable that is not reusable it will be exhausted after only the ngrams of length `1` have been found and `everygrams()` ends up returning only the ngrams of length `1`.

I am happy to make a PR for this.

## Non-Working Examples

For example, starting with the example from the documentation
```python
>>> from nltk import everygrams
>>> sent = 'a b c'.split()
>>> list(everygrams(sent))
[('a',), ('b',), ('c',), ('a', 'b'), ('b', 'c'), ('a', 'b', 'c')]
>>> list(everygrams(sent, max_len=2))
[('a',), ('b',), ('c',), ('a', 'b'), ('b', 'c')]
```

We should expect that `everygrams(iter(sent))` should have the same output as `everygrams(sent)`. Instead, it raises a `TypeError`.
```python
>>> list(everygrams(sent))
[('a',), ('b',), ('c',), ('a', 'b'), ('b', 'c'), ('a', 'b', 'c')]
>>> list(everygrams(iter(sent)))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/.../python3.8/site-packages/nltk/util.py", line 603, in everygrams
    max_len = len(sequence)
TypeError: object of type 'list_iterator' has no len()
```
Likewise, we should expect that `everygrams(iter(sent), max_len=2)` should have the same output as `everygrams(sent, max_len=2)`. This is not the case.
```python
>>> list(everygrams(sent, max_len=2))
[('a',), ('b',), ('c',), ('a', 'b'), ('b', 'c')]
>>> list(everygrams(iter(sent), max_len=2))
[('a',), ('b',), ('c',)]
```


## Possible Solutions

There are several possible solutions:
1. Do not accept iterables.
    * Since nltk is commonly used to process large amounts of text data, iterables are the most natural and practical approach to avoid loading data into memory all at once.
2. If the argument is an iterable, cache it, computing its length in the process, in case it cannot be reused.
    * This would require loading the data into memory all at once, which should be avoided.
3. Use a `history` sliding window of size `max_len` to produce the ngrams. This strategy is similar to `history` in the implementation of ngram.
    * This would be my suggested approach since it provides the most functionality. However, it would have the drawback of changing the order of the output ngrams. For example, 
      ```python
      >>> list(old_everygrams(iter(sent)))
      [('a',), ('b',), ('c',), ('a', 'b'), ('b', 'c'), ('a', 'b', 'c')]
      >>> list(new_everygrams(iter(sent)))
      [('a',), ('a', 'b'), ('a', 'b', 'c'), ('b',), ('b', 'c'), ('c',)]
      ```

I am happy to make a PR for this.
diff --git a/nltk/test/unit/lm/test_preprocessing.py b/nltk/test/unit/lm/test_preprocessing.py
index c29855263..76e062792 100644
--- a/nltk/test/unit/lm/test_preprocessing.py
+++ b/nltk/test/unit/lm/test_preprocessing.py
@@ -14,15 +14,15 @@ class TestPreprocessing(unittest.TestCase):
         expected_train = [
             [
                 ("<s>",),
-                ("a",),
-                ("b",),
-                ("c",),
-                ("</s>",),
                 ("<s>", "a"),
+                ("a",),
                 ("a", "b"),
+                ("b",),
                 ("b", "c"),
+                ("c",),
                 ("c", "</s>"),
-            ]
+                ("</s>",)
+             ]
         ]
         expected_vocab = ["<s>", "a", "b", "c", "</s>"]
         train_data, vocab_data = padded_everygram_pipeline(2, [["a", "b", "c"]])
diff --git a/nltk/test/unit/test_util.py b/nltk/test/unit/test_util.py
new file mode 100644
index 000000000..909dfecf7
--- /dev/null
+++ b/nltk/test/unit/test_util.py
@@ -0,0 +1,72 @@
+# -*- coding: utf-8 -*-
+"""
+Unit tests for nltk.util.
+"""
+
+import unittest
+from nltk.util import everygrams
+
+
+class TestEverygrams(unittest.TestCase):
+
+    def setUp(self):
+        """Form test data for tests."""
+        self.test_data = iter('a b c'.split())
+
+    def test_everygrams_without_padding(self):
+        expected_output = [
+            ('a',),
+            ('a', 'b'),
+            ('a', 'b', 'c'),
+            ('b',),
+            ('b', 'c'),
+            ('c',),
+        ]
+        output = everygrams(self.test_data)
+        self.assertCountEqual(output, expected_output)
+
+    def test_everygrams_max_len(self):
+        expected_output = [('a',), ('a', 'b'), ('b',), ('b', 'c'),('c',),]
+        output = everygrams(self.test_data, max_len=2)
+        self.assertCountEqual(output, expected_output)
+
+    def test_everygrams_min_len(self):
+        expected_output = [('a', 'b'), ('b', 'c'), ('a', 'b', 'c'),]
+        output = everygrams(self.test_data, min_len=2)
+        self.assertCountEqual(output, expected_output)
+
+    def test_everygrams_pad_right(self):
+        expected_output = [
+            ('a',),
+            ('a', 'b'),
+            ('a', 'b', 'c'),
+            ('b',),
+            ('b', 'c'),
+            ('b', 'c', None),
+            ('c',),
+            ('c', None),
+            ('c', None, None),
+            (None,),
+            (None, None),
+            (None,),
+        ]
+        output = everygrams(self.test_data, max_len=3, pad_right=True)
+        self.assertCountEqual(output, expected_output)
+
+    def test_everygrams_pad_left(self):
+        expected_output = [
+            (None,),
+            (None, None),
+            (None, None, 'a'),
+            (None,),
+            (None, 'a'),
+            (None, 'a', 'b'),
+            ('a',),
+            ('a', 'b'),
+            ('a', 'b', 'c'),
+            ('b',),
+            ('b', 'c'),
+            ('c',),
+        ]
+        output = everygrams(self.test_data, max_len=3, pad_left=True)
+        self.assertCountEqual(output, expected_output)

pip install -e .[all]
parse_log_pytest
requirements.txt
nose coverage pylint scipy numpy matplotlib scikit-learn python-crfsuite gensim pyparsing twython regex pytest
apt-get update apt-get install -y gcc
3.7
pip-req.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
astroid==2.15.8
cachetools==5.5.2
certifi @ file:///croot/certifi_1671487769961/work/certifi
chardet==5.2.0
charset-normalizer==3.4.1
click==8.1.8
colorama==0.4.6
coverage==7.2.7
cycler==0.11.0
dill==0.3.7
distlib==0.3.9
exceptiongroup==1.2.2
filelock==3.12.2
fonttools==4.38.0
gensim==4.2.0
idna==3.10
importlib-metadata==6.7.0
iniconfig==2.0.0
isort==5.11.5
joblib==1.3.2
kiwisolver==1.4.5
lazy-object-proxy==1.9.0
matplotlib==3.5.3
mccabe==0.7.0
-e git+https://github.com/nltk/nltk.git@0a88301f9362d1c0bf546705826b52ab2dd5add5#egg=nltk
nose==1.3.7
numpy==1.21.6
oauthlib==3.2.2
packaging==24.0
Pillow==9.5.0
platformdirs==4.0.0
pluggy==1.2.0
pylint==2.17.7
pyparsing==3.1.4
pyproject-api==1.5.3
pytest==7.4.4
python-crfsuite==0.9.10
python-dateutil==2.9.0.post0
regex==2024.4.16
requests==2.31.0
requests-oauthlib==2.0.0
scikit-learn==1.0.2
scipy==1.7.3
six==1.17.0
smart-open==7.1.0
threadpoolctl==3.1.0
tomli==2.0.1
tomlkit==0.12.5
tox==4.8.0
tqdm==4.67.1
twython==3.9.1
typed-ast==1.5.5
typing_extensions==4.7.1
urllib3==2.0.7
virtualenv==20.26.6
wrapt==1.16.0
zipp==3.15.0

name: nltk
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2022.12.7=py37h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - pip=22.3.1=py37h06a4308_0
  - python=3.7.16=h7a1cb2a_0
  - readline=8.2=h5eee18b_0
  - setuptools=65.6.3=py37h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - wheel=0.38.4=py37h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - astroid==2.15.8
      - cachetools==5.5.2
      - chardet==5.2.0
      - charset-normalizer==3.4.1
      - click==8.1.8
      - colorama==0.4.6
      - coverage==7.2.7
      - cycler==0.11.0
      - dill==0.3.7
      - distlib==0.3.9
      - exceptiongroup==1.2.2
      - filelock==3.12.2
      - fonttools==4.38.0
      - gensim==4.2.0
      - idna==3.10
      - importlib-metadata==6.7.0
      - iniconfig==2.0.0
      - isort==5.11.5
      - joblib==1.3.2
      - kiwisolver==1.4.5
      - lazy-object-proxy==1.9.0
      - matplotlib==3.5.3
      - mccabe==0.7.0
      - nose==1.3.7
      - numpy==1.21.6
      - oauthlib==3.2.2
      - packaging==24.0
      - pillow==9.5.0
      - platformdirs==4.0.0
      - pluggy==1.2.0
      - pylint==2.17.7
      - pyparsing==3.1.4
      - pyproject-api==1.5.3
      - pytest==7.4.4
      - python-crfsuite==0.9.10
      - python-dateutil==2.9.0.post0
      - regex==2024.4.16
      - requests==2.31.0
      - requests-oauthlib==2.0.0
      - scikit-learn==1.0.2
      - scipy==1.7.3
      - six==1.17.0
      - smart-open==7.1.0
      - threadpoolctl==3.1.0
      - tomli==2.0.1
      - tomlkit==0.12.5
      - tox==4.8.0
      - tqdm==4.67.1
      - twython==3.9.1
      - typed-ast==1.5.5
      - typing-extensions==4.7.1
      - urllib3==2.0.7
      - virtualenv==20.26.6
      - wrapt==1.16.0
      - zipp==3.15.0
prefix: /opt/conda/envs/nltk

nltk/test/unit/lm/test_preprocessing.py::TestPreprocessing::test_padded_everygram_pipeline nltk/test/unit/test_util.py::TestEverygrams::test_everygrams_max_len nltk/test/unit/test_util.py::TestEverygrams::test_everygrams_min_len nltk/test/unit/test_util.py::TestEverygrams::test_everygrams_pad_left nltk/test/unit/test_util.py::TestEverygrams::test_everygrams_pad_right nltk/test/unit/test_util.py::TestEverygrams::test_everygrams_without_padding
AUTHORS.md nltk/util.py nltk/lm/__init__.py
AUTHORS.md nltk/util.py nltk/lm/__init__.py