Python Forum
Using list comprehension with 'yield' in function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using list comprehension with 'yield' in function
#2
(Apr-02-2023, 05:25 AM)tester_V Wrote: I'm clearly doing something wrong
Your generator is generating a single item which is the whole list of subdirectories.

You could return the whole generator
def scan_dir(dir):
    return (str(x) for x in dir.iterdir() if x.is_dir())
or
import os
def scan_dir(dir):
    for name in next(os.walk(dir))[1]:
        yield os.path.join(dir, name)
or
import os
def scan_dir(dir):
    return (x.path for x in os.scandir(dir) if x.is_dir())
I think the last one is better.
tester_V likes this post
Reply


Messages In This Thread
RE: Using list comprehension with 'yield' in function - by Gribouillis - Apr-02-2023, 06:18 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  yield from akbarza 4 431 Apr-19-2024, 07:58 PM
Last Post: DeaD_EyE
  List Comprehension Issue johnywhy 5 632 Jan-14-2024, 07:58 AM
Last Post: Pedroski55
  yield usage as statement or expression akbarza 5 916 Oct-23-2023, 11:43 AM
Last Post: Gribouillis
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 529 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  list comprehension 3lnyn0 4 1,490 Jul-12-2022, 09:49 AM
Last Post: DeaD_EyE
  List comprehension used differently coder_sw99 3 1,785 Oct-03-2021, 04:12 PM
Last Post: coder_sw99
  How to invoke a function with return statement in list comprehension? maiya 4 2,947 Jul-17-2021, 04:30 PM
Last Post: maiya
  List comprehension and Lambda cametan 2 2,291 Jun-08-2021, 08:29 AM
Last Post: cametan
  What is the difference between a generator and a list comprehension? Pedroski55 2 2,281 Jan-02-2021, 04:24 AM
Last Post: Pedroski55
  For Loop with List Comprehension muzikman 25 6,941 Dec-18-2020, 10:45 PM
Last Post: muzikman

Forum Jump:

User Panel Messages

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