# Buggy code
def build_inventory(self):
    """Build Ansible inventory of droplets"""
    self.inventory = {
        "all": {"hosts": [], "vars": self.group_variables},
        "_meta": {"hostvars": {}},
    }

    # add all droplets by id and name
    for droplet in self.data["droplets"]:
        for net in droplet["networks"]["v4"]:
            if net["type"] == "public":
                dest = net["ip_address"]
            else:
                continue

        self.inventory["all"]["hosts"].append(dest)

        self.inventory[droplet["id"]] = [dest]
        self.inventory[droplet["name"]] = [dest]

        # groups that are always present
        for group in (
            "digital_ocean",
            "region_" + droplet["region"]["slug"],
            "image_" + str(droplet["image"]["id"]),
            "size_" + droplet["size"]["slug"],
            "distro_" + DigitalOceanInventory.to_safe(droplet["image"]["distribution"]),
            "status_" + droplet["status"],
        ):
            if group not in self.inventory:
                self.inventory[group] = {"hosts": [], "vars": {}}
            self.inventory[group]["hosts"].append(dest)

        # groups that are not always present
        for group in (droplet["image"]["slug"], droplet["image"]["name"]):
            if group:
                image = "image_" + DigitalOceanInventory.to_safe(group)
                if image not in self.inventory:
                    self.inventory[image] = {"hosts": [], "vars": {}}
                self.inventory[image]["hosts"].append(dest)

        if droplet["tags"]:
            for tag in droplet["tags"]:
                if tag not in self.inventory:
                    self.inventory[tag] = {"hosts": [], "vars": {}}
                self.inventory[tag]["hosts"].append(dest)

        # hostvars
        info = self.do_namespace(droplet)
        self.inventory["_meta"]["hostvars"][dest] = info


# Traceback (TypeError)
Traceback (most recent call last):
File "./digital_ocean.py", line 544, in <module>
DigitalOceanInventory()
File "./digital_ocean.py", line 309, in __init__
self.build_inventory()
File "./digital_ocean.py", line 479, in build_inventory
self.inventory[tag]['hosts'].append(dest)
TypeError: list indices must be integers, not str

# Fixed code
def build_inventory(self):
    """Build Ansible inventory of droplets"""
    self.inventory = {
        "all": {"hosts": [], "vars": self.group_variables},
        "_meta": {"hostvars": {}},
    }

    # add all droplets by id and name
    for droplet in self.data["droplets"]:
        for net in droplet["networks"]["v4"]:
            if net["type"] == "public":
                dest = net["ip_address"]
            else:
                continue

        self.inventory["all"]["hosts"].append(dest)

        self.add_host(droplet["id"], dest)

        self.add_host(droplet["name"], dest)

        # groups that are always present
        for group in (
            "digital_ocean",
            "region_" + droplet["region"]["slug"],
            "image_" + str(droplet["image"]["id"]),
            "size_" + droplet["size"]["slug"],
            "distro_" + DigitalOceanInventory.to_safe(droplet["image"]["distribution"]),
            "status_" + droplet["status"],
        ):
            self.add_host(group, dest)

        # groups that are not always present
        for group in (droplet["image"]["slug"], droplet["image"]["name"]):
            if group:
                image = "image_" + DigitalOceanInventory.to_safe(group)
                self.add_host(image, dest)

        if droplet["tags"]:
            for tag in droplet["tags"]:
                self.add_host(tag, dest)

        # hostvars
        info = self.do_namespace(droplet)
        self.inventory["_meta"]["hostvars"][dest] = info