Python Forum
When did the number got included in the list?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
When did the number got included in the list?
#14
(Feb-02-2022, 07:29 PM)Frankduc Wrote: But they still use the key word "def" and without the "yield" there is no generator. One would think they could of created some kind of method or class to represent the Generators.
Can make a generator and not using a function,bye making a generator expression.
>>> squares = (n** 2 for n in range(5))
>>> squares
<generator object <genexpr> at 0x0000019FB47C6510>

>>> list(squares)
[0, 1, 4, 9, 16]
From outside of function there is also Iterator.
>>> lst = [1, 2, 3]
>>> it = iter(lst)
>>> it
<list_iterator object at 0x0000019FB48430A0>
>>> next(it)
1
>>> next(it)
2
>>> next(it)
3
>>> list(it)

>>> it = iter(lst)
>>> it
<list_iterator object at 0x0000019FB47AC040>
>>> list(it)
[1, 2, 3]
So work it similar way,in fact so a Generator a subclass of Iterator.

The main strategy of this is Lazy Evaluation.
A evaluation strategy which delays the evaluation of an expression until it's value is needed,
and also avoids repeated evaluations.

When moved from Python 2 to 3 was made a lot stuff was rewritten to use this strategy.
# Python 2.7
>>> range(5)
[0, 1, 2, 3, 4]

>>> zip(range(5), range(5))
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]
Python 2 return a finish made list.
# Python 3.10
>>> range(5)
range(0, 5)
>>> list(range(5))
[0, 1, 2, 3, 4]

 >>> zip(range(5), range(5))
<zip object at 0x0000019FB41B27C0>
>>> list(zip(range(5), range(5)))
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]
In Python 3 it's lazy evaluated,so have to iterate over it to get result or call list() or other function like sum().
>>> sum(range(5))
10
Reply


Messages In This Thread
RE: When did the number got included in the list? - by snippsat - Feb-03-2022, 12:17 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Delete strings from a list to create a new only number list Dvdscot 8 1,660 May-01-2023, 09:06 PM
Last Post: deanhystad
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,402 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  The included URLconf 'scribimus.urls' does not appear to have any patterns in it. nengkya 0 1,098 Mar-03-2023, 08:29 PM
Last Post: nengkya
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,984 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Split a number to list and list sum must be number sunny9495 5 2,385 Apr-28-2022, 09:32 AM
Last Post: Dexty
  Divide a number by numbers in a list. Wallen 7 8,169 Feb-12-2022, 01:51 PM
Last Post: deanhystad
  Count number of occurrences of list items in list of tuples t4keheart 1 2,418 Nov-03-2020, 05:37 AM
Last Post: deanhystad
  How do I add a number to every item in a list? john316 2 2,022 Oct-28-2020, 05:29 PM
Last Post: deanhystad
  Print the number of items in a list on ubuntu terminal buttercup 2 1,979 Jul-24-2020, 01:46 PM
Last Post: ndc85430
  Make an array of string number in a List polantas 5 3,174 May-27-2020, 07:18 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020