List all files in directory `dirname`, option to filter on file extension

def list_files(dirname, extension=None):
    """
    List all files in directory `dirname`, option to filter on file extension
    """
    f = []
    for (dirpath, dirnames, filenames) in os.walk(dirname):
        f.extend(filenames)
        break
    if extension is not None:
        # Filter on extension
        filtered = []
        for filename in f:
            fn, ext = os.path.splitext(filename)
            if ext.lower() == '.' + extension.lower():
                filtered.append(filename)
        f = filtered
    return f