Skip to content

Instantly share code, notes, and snippets.

@crodriguez1a
Last active September 20, 2019 13:06

Revisions

  1. crodriguez1a revised this gist Sep 20, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions lesson_generator_expressions.py
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,11 @@
    ""
    """
    So what’s the difference between Generator Expressions and List Comprehensions?
    The generator yields one item at a time and generates item only when in demand.
    Whereas, in a list comprehension, Python reserves memory for the whole list.
    Thus we can say that the generator expressions are memory efficient than the lists.
    Ref: https://www.geeksforgeeks.org/python-list-comprehensions-vs-generator-expressions/
    ""
    """

    from sys import getsizeof

  2. crodriguez1a created this gist Sep 20, 2019.
    21 changes: 21 additions & 0 deletions lesson_generator_expressions.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    ""
    So whats the difference between Generator Expressions and List Comprehensions?
    The generator yields one item at a time and generates item only when in demand.
    Whereas, in a list comprehension, Python reserves memory for the whole list.
    Thus we can say that the generator expressions are memory efficient than the lists.

    Ref: https://www.geeksforgeeks.org/python-list-comprehensions-vs-generator-expressions/
    ""

    from sys import getsizeof

    comp = [i for i in range(10000)]
    gen = (i for i in range(10000))

    #gives size for list comprehension
    x = getsizeof(comp)
    print("x = ", x)

    #gives size for generator expression
    y = getsizeof(gen)
    print("y = ", y)