Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
yield from
#4
def my_join(*iterables, my_joiner=None):
    if not iterables:
        return
    #yield from iterables[0]
    # The same as the loop under
    for item in iterables[0]:
         yield item
    for iterable in iterables[1:]:
        yield my_joiner
        yield from iterable

list1 = list(my_join([1, 2, 3], [4, 5], [6, 7], my_joiner=9999))
print(list1)
list2 = list(my_join([1, 2, 3], [4, 5], [6, 7], my_joiner='***'))
print(list2)
# Now i have made a default argument <None> so now dos this work
list3 = list(my_join([1, 2, 3], [4, 5], [6, 7]))
print(list3)
Output:
[1, 2, 3, 9999, 4, 5, 9999, 6, 7] [1, 2, 3, '***', 4, 5, '***', 6, 7] [1, 2, 3, None, 4, 5, None, 6, 7
yield from was new in Python 3.3.
As see in code i have comment it out,and write same code as a loop
Reply


Messages In This Thread
yield from - by akbarza - Apr-19-2024, 07:55 AM
RE: yield from - by Larz60+ - Apr-19-2024, 08:52 AM
RE: yield from - by deanhystad - Apr-19-2024, 01:22 PM
RE: yield from - by snippsat - Apr-19-2024, 02:44 PM
RE: yield from - by DeaD_EyE - Apr-19-2024, 07:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  yield usage as statement or expression akbarza 5 1,001 Oct-23-2023, 11:43 AM
Last Post: Gribouillis
  Using list comprehension with 'yield' in function tester_V 5 1,479 Apr-02-2023, 06:31 PM
Last Post: tester_V
  Yield generator weird output Vidar567 8 3,430 Nov-23-2020, 10:59 PM
Last Post: deanhystad
  Trying to access next element using Generator(yield) in a Class omm 2 2,101 Oct-19-2020, 03:36 PM
Last Post: omm
  Yield statement question DPaul 6 2,647 Sep-26-2020, 05:18 PM
Last Post: DPaul
  Problem about yield, please help!! cls0724 5 3,036 Apr-08-2020, 05:37 PM
Last Post: deanhystad
  does yield support variable args? Skaperen 0 1,736 Mar-03-2020, 02:44 AM
Last Post: Skaperen
  generator function that yield from a list buran 9 4,436 Jun-04-2019, 10:26 PM
Last Post: snippsat
  yield help chakox 5 3,418 Apr-13-2019, 09:42 PM
Last Post: chakox
  Multiple calls to Python interpreter embedded in C++ application yield segmentation f mmoelle1 0 2,914 Mar-21-2019, 08:54 PM
Last Post: mmoelle1

Forum Jump:

User Panel Messages

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