Loop filling a set with logging After watching Raymond Hettingers talk Transforming Code into Beautiful, Idiomatic Python I got back to a function I wrote.

I'm not quite sure how to make it more pythonic but I think this might be a use-case to use the map function.

import logging
import string
import os

def _mounted_drives(possible_device_letters=string.ascii_uppercase):
    """Returns a set of mounted drive letters on a Windows machine."""
    result = set()
    for letter in possible_device_letters:
        mounted = os.path.exists(letter + ':')
        if mounted:
            logging.info('Found device ' + letter)
            result.add(letter)

    return result

if __name__ == '__main__':
    print(_mounted_drives())
Removing the logging and using set comprehension :

def _mounted_drives(possible_device_letters=string.ascii_uppercase):
    return {l for l in possible_device_letters if os.path.exists(l + ':')}


If you do want the logging but you do not care about it being done once every device is found :

def _mounted_drives(possible_device_letters=string.ascii_uppercase):
    s = {l for l in possible_device_letters if os.path.exists(l + ':')}
    for l in s:
        logging.info('Found device ' + l)
    return s