Handling hierarchical Data using Dictionaries for beginners (with python code)

 

Handling Hierarchical Data with Python Dictionaries

1. Introduction to Hierarchical Data

Hierarchical data is organized into a tree-like structure with different levels of depth. Each level contains unique objects that are connected to objects in the level above. This type of data is common in various domains, such as organizational structures, file systems, and biological classifications.

2. Example Data

We'll create a 5-level hierarchical data structure. The data represents an organizational hierarchy with departments, teams, managers, employees, and tasks.

    
data = {
    "Department1": {
        "TeamA": {
            "Manager1": {
                "Employee1": {
                    "Task1": "Complete report",
                    "Task2": "Attend meeting"
                },
                "Employee2": {
                    "Task1": "Write code",
                    "Task2": "Review PR"
                }
            },
            "Manager2": {
                "Employee3": {
                    "Task1": "Design UI",
                    "Task2": "Test application"
                },
                "Employee4": {
                    "Task1": "Deploy app",
                    "Task2": "Monitor system"
                }
            }
        },
        "TeamB": {
            "Manager3": {
                "Employee5": {
                    "Task1": "Market research",
                    "Task2": "Create content"
                },
                "Employee6": {
                    "Task1": "Plan event",
                    "Task2": "Handle logistics"
                }
            },
            "Manager4": {
                "Employee7": {
                    "Task1": "Customer support",
                    "Task2": "Troubleshoot issues"
                },
                "Employee8": {
                    "Task1": "Analyze data",
                    "Task2": "Generate insights"
                }
            }
        }
    },
    "Department2": {
        "TeamC": {
            "Manager5": {
                "Employee9": {
                    "Task1": "Develop strategy",
                    "Task2": "Meet stakeholders"
                },
                "Employee10": {
                    "Task1": "Research competitors",
                    "Task2": "Prepare presentations"
                }
            },
            "Manager6": {
                "Employee11": {
                    "Task1": "Design marketing plan",
                    "Task2": "Track campaigns"
                },
                "Employee12": {
                    "Task1": "Conduct surveys",
                    "Task2": "Analyze feedback"
                }
            }
        },
        "TeamD": {
            "Manager7": {
                "Employee13": {
                    "Task1": "Prepare financial reports",
                    "Task2": "Budget analysis"
                },
                "Employee14": {
                    "Task1": "Audit accounts",
                    "Task2": "Compliance check"
                }
            },
            "Manager8": {
                "Employee15": {
                    "Task1": "IT support",
                    "Task2": "Maintain systems"
                },
                "Employee16": {
                    "Task1": "Update software",
                    "Task2": "Security checks"
                }
            }
        }
    }
}
    
    

3. Building the Dictionary

To build the dictionary, we can use nested dictionaries for each level of the hierarchy. Here is the Python code to construct the hierarchical data:

    
# Initialize the hierarchical data structure
data = {}

# Adding departments
data["Department1"] = {}
data["Department2"] = {}

# Adding teams to Department1
data["Department1"]["TeamA"] = {}
data["Department1"]["TeamB"] = {}

# Adding managers to TeamA in Department1
data["Department1"]["TeamA"]["Manager1"] = {}
data["Department1"]["TeamA"]["Manager2"] = {}

# Adding employees to Manager1 in TeamA
data["Department1"]["TeamA"]["Manager1"]["Employee1"] = {
    "Task1": "Complete report",
    "Task2": "Attend meeting"
}
data["Department1"]["TeamA"]["Manager1"]["Employee2"] = {
    "Task1": "Write code",
    "Task2": "Review PR"
}

# Similarly add other managers, employees, and tasks...

# Adding teams to Department2
data["Department2"]["TeamC"] = {}
data["Department2"]["TeamD"] = {}

# Adding managers to TeamC in Department2
data["Department2"]["TeamC"]["Manager5"] = {}
data["Department2"]["TeamC"]["Manager6"] = {}

# Adding employees to Manager5 in TeamC
data["Department2"]["TeamC"]["Manager5"]["Employee9"] = {
    "Task1": "Develop strategy",
    "Task2": "Meet stakeholders"
}
data["Department2"]["TeamC"]["Manager5"]["Employee10"] = {
    "Task1": "Research competitors",
    "Task2": "Prepare presentations"
}

# Similarly add other managers, employees, and tasks...
    
    

4. Accessing Dictionary Objects with Loops

To access the hierarchical data, we can use nested loops to traverse the dictionary structure.

    
# Access and print all tasks for each employee in the hierarchy
for department, teams in data.items():
    print(f"Department: {department}")
    for team, managers in teams.items():
        print(f"  Team: {team}")
        for manager, employees in managers.items():
            print(f"    Manager: {manager}")
            for employee, tasks in employees.items():
                print(f"      Employee: {employee}")
                for task, description in tasks.items():
                    print(f"        {task}: {description}")
    
    

5. Creating Reports by Grouping Data

We can create various reports by grouping the data based on different levels of the hierarchy. Here is an example of creating a report that lists tasks for each department.

    
# Create a report of tasks for each department
department_report = {}

for department, teams in data.items():
    department_report[department] = []
    for team, managers in teams.items():
        for manager, employees in managers.items():
            for employee, tasks in employees.items():
                for task, description in tasks.items():
                    department_report[department].append(description)

# Print the report
for department, tasks in department_report.items():
    print(f"\nTasks for {department}:")
    for task in tasks:
        print(f"  - {task}")
     

Support Our Efforts and Earn Together ðŸš€

Visit https://parucodes.github.io/ today and start your journey to becoming a fast, accurate, and confident touch typist.

If you find our website useful and want to support us, consider joining the exciting world of Bitcoin mining on your mobile phone. Follow this link: Mine PI Bitcoin and use my username prarthanadp as your invitation code. With the referral code prarthanadp, you'll receive a special referral bonus.

Thank you for your support! Let's grow and earn together! 🌟

 

Comments

Post a Comment

Popular posts from this blog

Recursion examples for Beginners (step by step code execution walkthrough with python code)

Word Search in Maze using Depth First Search (with python code and step by step code execution walkthrough)