Ravaan Techky

Ravaan Techky Group invites all Techkies.

Python

Generator:

  • Only returning value which requires operation and keep track of previous value to compute next value.
  • Memory Usage is optimum than List.
  • Could be use in less hardware configuration.

Example

  • Creating simple function which returning cube of given number
    def get_cube(n):
      result = []
      for t in range(n):
          result.appen(t**3)
      return result
    

    Input:

      get_cube(5)
    

    Output:

    [0, 1, 8, 27, 64]
    

    Input:

      result = get_cube(5)
      for number in result:
          print(number)
    

    Output:

    0
    1
    8
    27
    64
    

    In above get_cube function, we are creating result array and then returning it from function.

def get_cube(n):
	for t in range(n):
		yield t

Input:

	get_cube(5)

Output:

<generator object get_cube at 0x000002B219DFF660>

Input:

	result = get_cube(5)
	for number in result:
		print(number)

Output:

0
1
8
27
64

In above get_cube function, we are not creating result array and generate one value at a time.

### next function On Generator object we can use next function to return next value from result. Example, -

 def get_cube(n):
    for num in range(n):
        yield num **3

Input:

 res = get_cube(5)
 print(next(res))
 print(next(res))
 print(next(res))

Output:

 0
 1
 8

next function give error in case if object is not type of generator Example, -

def get_cube(n):
    result = []
    for num in range(n):
        result.append(num ** 3)
    return result

Input:

 res = get_cube(5)
 print(next(res))

Output:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-9-99bd7a0c47e0> in <module>
----> 1 next(res)

TypeError: 'list' object is not an iterator

To provide ‘list’ object an iterator, we can use iter method Example, -

def get_cube(n):
    result = []
    for num in range(n):
        result.append(num ** 3)
    return result

Input:

 res = get_cube(5)
 new_res = iter(res)
 print(next(new_res))
 print(next(new_res))
 print(next(new_res))

Output:

 0
 1
 8



Back


python-documentation is maintained by ravaan-techky.