Home > Net >  deleting a value from dictionary affetcs other values in the dictionary
deleting a value from dictionary affetcs other values in the dictionary

Time:09-07

I have a dictionary in python which looks like this

my_dict = {
    '100':[['a', [10], [5]]],
    '101':[['a', [10], [7]]],
    '102':[['a', [10], [11]]],
    '103':[['a', [10], [4]]], 
}

To create my_dict,

my_dict = dict()
id_nums = ['100', '101', '102', '103', ...]
for id in id_nums:
   info_list = list()
   ## code to populate info list
   # based on certain conditions
   if some_condition:
       info_list.insert(0, 'a')
       info_list.insert(1, list1) # list_1 is passsed from another function
   else:
       info_list.insert(0, 'a')
       info_list.insert(1, [])
   if other_condition:
       info_list.insert(2, list2) # list2 is being passed from other function
   else:
       info_list.insert(2, [])
   if id not in my_dict.keys():
       my_dict[id] = list()
   my_dict[id].append(info_list)

Where info list contains ['a', [10], [x].

I'm looping over all the keys in the dict and my goal is to delete number at 1st index in each value i.e. 10. To achieve this I have:

for k in my_dict.keys():
    my_dict[k][1].remove(10)

When I execute above code, at the very first key i.e. '100' the remove method also deletes 10 from the values of all the keys in my_dict.

Can anybody help me understand why this happens? and also how it can be avoided?

Thanks

CodePudding user response:

Your question is a good example of why you need to show us actual code, rather than sterilized and re-typed code. My bet is that what you actually wrote is more like this:

lst = [10]
my_dict = {
    '100':['a', lst, [5]],
    '101':['a', lst, [7]],
    '102':['a', lst, [11],
    '103':['a', lst, [4]], 
}

In that case, if you did my_dict['100'][1].remove(10), that WOULD affect all of the keys in your dictionary, because you don't have four separate lists. You have four references to the SAME list. Changing that list changes all the views of that list.

CodePudding user response:

I am not sure about what you are looking for but I will give you several scenarios on how to delete objects from your dictionary

scenario 1

To delete the value in index 1 from the first key (in this case key='100') in the dictionary

my_dict = {
    '100':[['a'], [10], [5]],
    '101':[['a'], [10], [7]],
    '102':[['a'], [10], [11]],
    '103':[['a'], [10], [4]], 
}
k = list(my_dict.keys())[0]
del my_dict[k][1]
print(my_dict)

output

{'100': [['a'], [5]], '101': [['a'], [10], [7]], '102': [['a'], [10], [11]], '103': [['a'], [10], [4]]}

scenario 2

To delete the value in index 0 for the list in index 1 from the first key in the dictionary.

my_dict = {
    '100':[['a'], [10], [5]],
    '101':[['a'], [10], [7]],
    '102':[['a'], [10], [11]],
    '103':[['a'], [10], [4]], 
}
k = list(my_dict.keys())[0]
del my_dict[k][1][0]
print(my_dict)

output

{'100': [['a'], [], [5]], '101': [['a'], [10], [7]], '102': [['a'], [10], [11]], '103': [['a'], [10], [4]]}

scenario 3

To delete the values in index 1 for all keys in the dictionary.

my_dict = {
    '100':[['a'], [10], [5]],
    '101':[['a'], [10], [7]],
    '102':[['a'], [10], [11]],
    '103':[['a'], [10], [4]], 
}
for k in my_dict:
    del my_dict[k][1]

print(my_dict)

output

{'100': [['a'], [5]], '101': [['a'], [7]], '102': [['a'], [11]], '103': [['a'], [4]]}

scenario 4

To delete the first value in the index 1 for all keys in the dictionary.

my_dict = {
    '100':[['a'], [10], [5]],
    '101':[['a'], [10], [7]],
    '102':[['a'], [10], [11]],
    '103':[['a'], [10], [4]], 
}
for k in my_dict:
    del my_dict[k][1][0]
print(my_dict)

output

{'100': [['a'], [], [5]], '101': [['a'], [], [7]], '102': [['a'], [], [11]], '103': [['a'], [], [4]]}
  • Related