Hi,
I was trying to the exercise 28, from the website and came across a doubt and a error in my code.
Exercise 28 in a gist
We have been given a "table" it is a list of dictionaries
table = [{'name': 'Sophie', 'year': 1973 ,'tel': 5553546},
{'name': 'Bruno', 'year': 1981 ,'tel': 5558432}]
along with this we will have a column name "col" and a value as "val", we need to remove all the mentioned "col" from a new_table (we cannot modify the original table) and all the rows with a diffrent "val" in the associated "col"
Doubt-
As we cannot change the original list, I know we cannot use .copy(), list slicing or list reassignment as they will ref to the same obejct and not create a new one
To overcome this I made an empty_table and appned the values of table in it.
Thus by doing this am I creating a new object?
Error -
my code -
def es28(table, column, value):
table_2=[]
for i in table:
table_2.append(i)
row_del=0
count=0
for i in range(len(table_2)):
if not table_2[i-count][column]== value:
del table_2[i-count]
count+=1
row_del=row_del+1
else:
pass
print("Value of table right now {}".format(table))
for j in range(len(table_2)):
del table_2[j][column]
#the value of table changes here
print("Value of table after now after for del {}".format(table))
es28([{'name': 'Sophie', 'year': 1973 ,'tel': 5553546},
{'name': 'Bruno', 'year': 1981 ,'tel': 5558432}], 'year', 1981)
The error :- When I run the code the original value of table is modified after the second for loop (not the first one though). If we run the code we will see the "year" column has been deleted after the code goes thru the second for loop.
I am not referencing the original table anywhere in the second for loop and cannot understand how the original table has changed.
Any help would be appreciated
Thank you