confusion in python printing the lists -
in following python code, have list i.e. to_do_list of 2 lists i.e. other_events , grocery_list inserted item in grocery_list , deleted it.
my confusion is, when updated grocery_list, to_do_list automatically updated when deleted grocery_list, to_do_list not updated... why?
grocery_list = ['banana', 'mango', 'apple'] other_events =['pick kids','do laundry', 'dance class'] to_do_list = [other_events, grocery_list] print(to_do_list) grocery_list.insert(1,'onions'); print(grocery_list) del grocery_list print(to_do_list) its output is:
[['pick kids', 'do laundry', 'dance class'], ['banana', 'mango', 'apple']] ['banana', 'onions', 'mango', 'apple'] [['pick kids', 'do laundry', 'dance class'], ['banana', 'onions', 'mango', 'apple']]
the reason why, when update grocery_list object to_do_list updated list to_do_list contain reference on grocery_list.
now, why after deleting grocery_list, to_do_list not updated keywork del doesn't delete object on delete grocery_list namespace.
here quote python language reference on del :
deletion of name removes binding of name local or global namespace
Comments
Post a Comment