Append is overwriting existing dictionary in list
Question:
I’m pretty new to python, and I have a python program that is supposed to take inputs and store them into a dictionary and store that dictionary into a list, but for some reason, it’s overwriting the stored dictionary in the list with the new one at each loop.
total_properties_sold = 0
total_commission = 0
bonus_pay = 0
employees = [] #empty list to store dictonary of employees information
employee = {} #empty dictonary to store the information of employees
while True:
number_of_employees = input("Enter number of employees who have worked this week: ")
if number_of_employees.isdigit(): #validation check to ensure input is a digit
number_of_employees = int(number_of_employees) #if input is digit, convert to int
if number_of_employees >= 2 and number_of_employees <= 5: #validates the range of input
break
else:
print("Input invalid... enter a digitn")
else:
print("Input invalid... enter a digitn")
#for loop to enter the details of the number of employees
for number in range(number_of_employees):
name = input("Enter employee name: ")
while True: #validation check to ensure input is a digit
id = input("Enter employee id: ")
if id.isdigit():
id = int(id)
break
else:
print("Invalid data type... enter a numbern")
while True:
properties_sold = input("enter number of properties sold by employee: ")
if properties_sold.isdigit():
properties_sold = int(properties_sold)
break
else:
print("Invalid data type... enter a number")
total_properties_sold += properties_sold #calculates the total properties sold this week
employee_commission = properties_sold * 500
employee_commission = float(employee_commission) #converts commission into a float
total_commission += employee_commission #calculates the total commission this week
total_commission = float(total_commission) #converts total commission into float
#add employee information into the employee dictonary
employee["name"] = name
employee["employee id"] = id
employee["properties sold"] = properties_sold
employee["commission"] = employee_commission
employees.append(employee) #adding the employee dictonary into employees list
print(employees)
The expected output should be
[{'name': 'employee1', 'employee id': 1, 'properties sold': 4, 'commission': 2000.0}, {'name': 'employee2', 'employee id': 2, 'properties sold': 3, 'commission': 1500.0}, {'name': 'employee3', 'employee id': 3, 'properties sold': 10, 'commission': 5000.0}]
But I get:
[{'name': 'employee3', 'employee id': 3, 'properties sold': 10, 'commission': 5000.0}, {'name': 'employee3', 'employee id': 3, 'properties sold': 10, 'commission': 5000.0}, {'name': 'employee3', 'employee id': 3, 'properties sold': 10, 'commission': 5000.0}]
Answers:
This is because you assign employee = {}
at the beginning of the code, and it is never reassigned after that, so the code is always referring to the same object.
When you set employee["name"] = name
for the next employee, you’re still referring to the same employee
object, and it affects the instance of employee
that is already in the list.
To fix this, assign employee
to a fresh dictionary at the top of the for loop:
for number in range(number_of_employees):
employee = {}
name = input("Enter employee name: ")
This reassigns the variable name employee
to a fresh value, and breaks the connection to the previous values.
I’m pretty new to python, and I have a python program that is supposed to take inputs and store them into a dictionary and store that dictionary into a list, but for some reason, it’s overwriting the stored dictionary in the list with the new one at each loop.
total_properties_sold = 0
total_commission = 0
bonus_pay = 0
employees = [] #empty list to store dictonary of employees information
employee = {} #empty dictonary to store the information of employees
while True:
number_of_employees = input("Enter number of employees who have worked this week: ")
if number_of_employees.isdigit(): #validation check to ensure input is a digit
number_of_employees = int(number_of_employees) #if input is digit, convert to int
if number_of_employees >= 2 and number_of_employees <= 5: #validates the range of input
break
else:
print("Input invalid... enter a digitn")
else:
print("Input invalid... enter a digitn")
#for loop to enter the details of the number of employees
for number in range(number_of_employees):
name = input("Enter employee name: ")
while True: #validation check to ensure input is a digit
id = input("Enter employee id: ")
if id.isdigit():
id = int(id)
break
else:
print("Invalid data type... enter a numbern")
while True:
properties_sold = input("enter number of properties sold by employee: ")
if properties_sold.isdigit():
properties_sold = int(properties_sold)
break
else:
print("Invalid data type... enter a number")
total_properties_sold += properties_sold #calculates the total properties sold this week
employee_commission = properties_sold * 500
employee_commission = float(employee_commission) #converts commission into a float
total_commission += employee_commission #calculates the total commission this week
total_commission = float(total_commission) #converts total commission into float
#add employee information into the employee dictonary
employee["name"] = name
employee["employee id"] = id
employee["properties sold"] = properties_sold
employee["commission"] = employee_commission
employees.append(employee) #adding the employee dictonary into employees list
print(employees)
The expected output should be
[{'name': 'employee1', 'employee id': 1, 'properties sold': 4, 'commission': 2000.0}, {'name': 'employee2', 'employee id': 2, 'properties sold': 3, 'commission': 1500.0}, {'name': 'employee3', 'employee id': 3, 'properties sold': 10, 'commission': 5000.0}]
But I get:
[{'name': 'employee3', 'employee id': 3, 'properties sold': 10, 'commission': 5000.0}, {'name': 'employee3', 'employee id': 3, 'properties sold': 10, 'commission': 5000.0}, {'name': 'employee3', 'employee id': 3, 'properties sold': 10, 'commission': 5000.0}]
This is because you assign employee = {}
at the beginning of the code, and it is never reassigned after that, so the code is always referring to the same object.
When you set employee["name"] = name
for the next employee, you’re still referring to the same employee
object, and it affects the instance of employee
that is already in the list.
To fix this, assign employee
to a fresh dictionary at the top of the for loop:
for number in range(number_of_employees):
employee = {}
name = input("Enter employee name: ")
This reassigns the variable name employee
to a fresh value, and breaks the connection to the previous values.