Search code examples
pythondictionarynestedkey

How can I change int value of nested dictionary key? (Python 3)


I have to write a menu-based 'Employee Management' program for one my assessment pieces. One of the main menu items is 'Employee Details', with the following options:

  1. Show employees
  2. Add new employees
  3. Remove employees

I've already completed 1, & 2, but I'm stuck with a specific function in 3. Remove employees. When an employee is added, it's stored in the employees_all dictionary as such:

 {1: {'fullname': 'John Smith', 'phonenumber': '0400 000 000', 'designation': 'Technician'},
  2: {'fullname': 'Jane Doe', 'phonenumber': '0411 111 111', 'designation': 'Manager'},
  3: {'fullname': 'Jimmy Smithers', 'phonenumber': '0422 222 222', 'designation': 'Maintenance'}}
        

I deliberately assigned an ascending int key when an employee is added. To remove an employee, program takes the employee id (the key) as input, and deletes that employee from the dictionary.

What I want, is that when (for instance) employee #2 is deleted, I want employee #3's key to become 2, for employee #4's key to become 3, etc, so on and so forth. I've tried to create some for loops that subtract 1 from dictionary keys greater than the value of the deleted employee's key, but I keep getting: unsupported operand type(s) for -: 'dict' and 'int'

Each menu item has it's own function. For this one, it's remove_employee(). See code below:

def remove_employee():
    print("\n______________________________________________")
    print("\n> MAIN MENU > EMPLOYEE DATA > REMOVE EMPLOYEE")
    print("\n\nWhich employee would you like to delete?")
    delnum = int(input("\n\nEmployee Number: "))

    print("\n______________________________________________")
    print("\n\nARE YOU SURE YOU WANT TO DELETE EMPLOYEE #",delnum,"?")
    print("\n1. Yes\n2. No")
    areyousure1 = input("\nEnter choice:  ")
                
    if areyousure1 == "1":                    
        del(employees_all[delnum])
            
        #### PLEASE HELP HERE ####
                  
        print("\n______________________________________________")
        print("\nEMPLOYEE DATA SUCCESSFULLY DELETED")
        time.sleep(1)
        submenu1()  
    
    elif areyousure1 == "2":
        print("\n______________________________________________")
        print("\n--------- EMPLOYEE DATA NOT DELETED ----------")
        time.sleep(1)
        submenu1()

    else:
        print("\n______________________________________________")
        print("\nINVALID INPUT!  PLEASE ENTER A VALID NUMBER.")
        time.sleep(1)
        remove_employee()

Solution

  • Your data structure isn't optimal. The best way you could come with in my opinion is to use array and add ID field to your employee object.

    employees = [
       {"id": 1, 'fullname': 'John Smith', 'phonenumber': '0400 000 000', 'designation': 'Technician'},
       {"id": 2, 'fullname': 'Jane Doe', 'phonenumber': '0411 111 111', 'designation': 'Manager'},
       {"id": 3, 'fullname': 'Jimmy Smithers', 'phonenumber': '0422 222 222', 'designation': 'Maintenance'}
    ]
    
    
    def delete_employee(id):
       employees = [emp for emp in employees if emp.id != id]
       # the new employees list is without the id you have removed
    

    However, if you want to kee pthis data structure for any reason, you can use this code :

    
    employees = {1: ...}
    
    def delete_employee(id):
       employees = {emp_id, employees[id] for emp_id in employees if emp_id != id}
       # the new employees list without unwanted employee