'Convert dictionary of lists to dictionary of dictionary

Current output is of format, dictionary of lists

{
    "majestic-service-1.324.02070909": [
        "/home/robotics/arm-services/FeaturesDir.yaml",
        "/home/robotics/arm-service/majestic-service.tar.gz"
    ],
}

and I'm looking to change that output format to something like below.(dictionary of dictionary)

{
    "majestic-service-1.324.02070909": {
        "yaml_file": "/home/robotics/arm-services/FeaturesDir.yaml",
        "tar_file": "/home/robotics/arm-services/majestic-service-1.324.02070909.tar.gz",
        "kind": "FeaturesDir"
    }
}

Corresponding code snippet that I've tried,

 output_dict = {}
 for file in application_files:
    match = re.match(regex_pattern, os.path.basename(file))
    if match:
         if os.path.exists(os.path.join(os.path.dirname(file), "FeaturesDir.yaml")):
              output_dict[file_without_extension(match.string)] = {os.path.join(os.path.dirname(file), "FeaturesDir.yaml")}
              output_dict[file_without_extension(match.string)].append(file)
              output_dict["Kind"] = "FeaturesDir"
         elif os.path.exists(os.path.join(os.path.dirname(file), "output_path/Deviations.yaml")):
                output_dict[file_without_extension(match.string)] = {os.path.join(os.path.dirname(file), "output_path/Deviations")}
                output_dict[file_without_extension(match.string)].append(file)
                output_dict["Kind"] = "Deviations"

# where the function file_without_extension, will return - majestic-service-1.324.02070909 from majestic-service-1.324.02070909.tar.gz

It reports the following error

AttributeError: 'set' object has no attribute 'append'

What am I doing wrong ?



Solution 1:[1]

The error occurs because in this line output_dict[file_without_extension(match.string)] = {os.path.join(os.path.dirname(file), "output_path/Deviations")} you're using {} brackets which for python are intended as the data type called set. Sets don't have append attribute, they have add attribute instead but it's a completely different data type. That's why it shows you that kind of error!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 federicknellus