Please ignore secret bonuses. Secret tests do NOT award bonus. Max hw grade is 30+2 bonus efficiency

Do you need help?

Doubts and error in exercise 28

A
Archit (1360 points)
0 1 5
in Programming in Python by (1.4k points)
closed by

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  

171 views
closed

1 Answer

Best answer
andrea.sterbini (208020 points)
756 1270 2377
by (208k points)
selected by
you are appending the original rows to the new table, thus when you delete one of their items you modify the original rows.

you should copy each row before appending them
A
Archit (1360 points)
0 1 5
by (1.4k points)
I cannot understand how to copy the list and make a new object.
I tried using list slicing, .copy(), reassignment but all of them make a shallow copy of the list and do not create a new object.

I also searched and found .deepcopy() from the copy library but I am not allowed to use it, thus I tried to append it. But it seems it also makes a shallow copy.

So, I don't know any other ways that I can copy and make a new object.

I would kindly like to know how to make a  copy with a new object

Thank you very much
andrea.sterbini (208020 points)
756 1270 2377
by (208k points)

newlist = [ item.copy() for item in oldlist.items() ]