'I want to search for inputted employees and return them in a dictionary from this program

print('----------------------------------------------\n')
print('      Welcome to the Employee Data System    -\n')
print('----------------------------------------------\n')
lst1=[]
num=int(input("Enter number of employees:"))
for n in range(num):
    empno=int(input("Enter employee number:"))
    lst1.append(empno)
    print("The employee numbers are:",lst1)
lst2=[]
for i in range(num):
    empname=input("Enter name of employee:")
    lst2.append(empname)
    print("The Employee names are:",lst2)
dict1=dict(zip(lst1,lst2))
print("The employees are:",dict1)
while True:
    print("Press 1 for removing an employee")
    print("Press 2 for adding an employee")
    print("Press 3 for serching an employee")
    print("Press 4 for exiting the program")
    ch=int(input("Enter choice:"))
    if ch==1:
        rem=int(input("Enter employee number of employee u wish to remove:"))
        if rem in dict1:
            dict1.pop(rem)
            print(dict1)
    elif ch==2:
        empno2=int(input("Enter number of employees needed to be added"))
        for n in range(empno2):
            updno=int(input("Enter employee number:"))
            lst1.append(updno)
            print("The update employee numebrs are:",lst1)
        for n in range(empno2):
            updname=input("Enter name of employees to be added:")
            lst2.append(updname)
            print("The updated employee names are:",lst2)
        dict2=dict(zip(lst1,lst2))
        print("The updated employees are",dict2)
    elif ch==3:
        pass
    elif ch==4:
        break

I can't search for emloyee and return them in a dictionary or tabular column so I just want to add a feature that the user can input a number in keys and the program checks for the value, and if found then it returns in like dictionary. Or are there any other options regarding the search?



Solution 1:[1]

Creating a search is not very complicated. Especially in dictionaries. If you are using search by id, since id is the key, just use that to find it. Like:

# Let's say the dictionary is:
dict1 = {1:'Ace', 2:'Adam'}
# you can find employee number 2 just by saying
second_employee = dict1[2]

Also, Your program requires the user to enter all employee numbers together and all employees together which might create confusion. Though this isn't related to your code, you should change this and make it this way:

num=int(input("Enter number of employees:"))
for n in range(num):
    empno=int(input("Enter employee number:"))
    lst1.append(empno)
    print("The employee numbers are:",lst1)
lst2=[]
for i in range(num):
    empname=input("Enter name of employee:")
    lst2.append(empname)
    print("The Employee names are:",lst2)

Change this bit to:

num=int(input("Enter number of employees:"))
dict1 = {}
for n in range(num):
    empno=int(input("Enter employee number:"))
    empname=input("Enter name of employee:")
    dict1[empno] = empname

This will prevent usage of lst1, lst2 and the zip function which take time and make your code optimized. This is completely optional though.

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 Anshumaan Mishra