There is a dictionary: directories = { '1': ['2207 876234', '11-2'], '2': ['10006'], '3': [], '10': ['abc'] } Make a function that outputs all dictionary keys in the format: 1, 2, 3, 10
def make_list(direct): return ','.join(list(direct.keys())) make_list(directories)