Python Forum
generator function that yield from a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
generator function that yield from a list
#1
Let's say we have a string lie this 'a b c' (simplified example)
so we can do
spam = 'a b c'
for ch in spam.split(' '):
    print(ch)


in this case str.split() will produce a list (i.e. it will generate the whole list in memory)

for same result we can do also

def chars(eggs):
   for ch in eggs.split(' '):
       yield ch

spam = 'a b c'
for ch in chars(spam):
    print(ch)
in this case the generator function chars will not produce the list from eggs.split() in memory, right? it will be evaluated lazy? I started to doubt myself today answering on SO question... Blush
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
generator function that yield from a list - by buran - Jun-04-2019, 02:00 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  yield from akbarza 4 395 Apr-19-2024, 07:58 PM
Last Post: DeaD_EyE
  yield usage as statement or expression akbarza 5 912 Oct-23-2023, 11:43 AM
Last Post: Gribouillis
  Using list comprehension with 'yield' in function tester_V 5 1,337 Apr-02-2023, 06:31 PM
Last Post: tester_V
  Tuple generator, and function/class syntax quazirfan 3 3,992 Aug-10-2021, 09:32 AM
Last Post: buran
  What is the difference between a generator and a list comprehension? Pedroski55 2 2,279 Jan-02-2021, 04:24 AM
Last Post: Pedroski55
  Yield generator weird output Vidar567 8 3,352 Nov-23-2020, 10:59 PM
Last Post: deanhystad
  Creating list of lists from generator object t4keheart 1 2,241 Nov-13-2020, 04:59 AM
Last Post: perfringo
  list call problem in generator function using iteration and recursive calls postta 1 1,976 Oct-24-2020, 09:33 PM
Last Post: bowlofred
  Trying to access next element using Generator(yield) in a Class omm 2 2,033 Oct-19-2020, 03:36 PM
Last Post: omm
  Yield statement question DPaul 6 2,570 Sep-26-2020, 05:18 PM
Last Post: DPaul

Forum Jump:

User Panel Messages

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