1

``I have a dictionary in python like this.

dictionary = {"00":[1,2,3,4,5,6,7,8,9],"01":[1,2,3,4,5,6,7,8,9],"02":[1,2,3,4,5,6,7,8,9],"03":[1,2,3,4,5,6,7,8,9],"04":[1,2,3,4,5,6,7,8,9]........up-to "99":[1,2,3,4,5,6,7,8,9]}

I have to delete the value 2 from the list of "00".I tried it using following statement.

 del (dictionary[key][dictionary[key].index(sudokumatrix[i][iindex])]).

Here key has value "00" and sudokumatrix[i][iindex] has value 2.But i got resulting dictionary as

{"00":[1,3,4,5,6,7,8,9],"01":[1,3,4,5,6,7,8,9],"02":[1,3,4,5,6,7,8,9],"03":[1,3,4,5,6,7,8,9],"04":[1,3,4,5,6,7,8,9].....}.

I have to get the result as:

{"00":[1,3,4,5,6,7,8,9],"01":[1,2,3,4,5,6,7,8,9],"02":[1,2,3,4,5,6,7,8,9],"03":[1,2,3,4,5,6,7,8,9],"04":[1,2,3,4,5,6,7,8,9]....}

I am posting the whole code here:

dictionary = dict()
zerotonine = "123456789"
list2 = list(zerotonine)
list2 = [int(i) for i in list2]
sudokumatrix=[]
for p in range(9):
     for q in range(9):
           keyis=str(p)+str(q)
           dictionary[keyis] = list2 


for i in range(9):
     initialinput = [1,2,3,4,5,6,7,8,9]
     list1=list(initialinput)
     list1 = [int(i) for i in list1]
     sudokumatrix.append(list1)

key = "00"

del dictionary[key][dictionary[key].index(sudokumatrix[0][1])]


print dictionary
Gadheyan .t.s
  • 377
  • 2
  • 9
  • 23
  • Check out [Simple way to delete a list element by value](https://stackoverflow.com/questions/2793324/is-there-a-simple-way-to-delete-a-list-element-by-value-in-python) – LinkBerest Jul 19 '15 at 04:00
  • Failed to replicate with Python 2.7.6 – André Fratelli Jul 19 '15 at 04:01
  • 1
    You should probably post your whole code. At first sight this looks like interning, which is *not* the default behaviour for Python arrays. It's odd that the value would be removed from all arrays without interning. – André Fratelli Jul 19 '15 at 04:03

3 Answers3

1

EDIT == I guess(since the generation of dictionary is not given) ==EDIT. The reason is that the values of keys '00', '01', ... are pointing to the same list. Modifying one of them will definitely affect the others.

Try using this to generate your dict

dictionary = dict((str(x).zfill(2), range(1, 10)) for x in range(100))

Your code of this part is actually not wrong, but to use list.remove() will make it much better.

YuMS
  • 135
  • 1
  • 6
  • It's odd that they all point at the same reference. If he is generating the lists as shown on the post the lists should be different, at least in Python 2. I'm not sure whether Python 3 changed this. – André Fratelli Jul 19 '15 at 04:05
  • Why odd? `an_list_instance = range(1, 10); dictionary = dict((str(x).zfill(2), an_list_instance) for x in range(100))` – YuMS Jul 19 '15 at 04:08
  • I see, but he said the dict is just like that.. I guess it's not how it was generated. – YuMS Jul 19 '15 at 04:10
  • 1
    @AndréFratelli It would not do this in Python 3 either – user3636636 Jul 19 '15 at 04:15
  • This is really odd than. If the dictionary is being generated manually in that way this should not happen. – André Fratelli Jul 19 '15 at 04:26
1

The issue has to do with pointers.

replace this:

dictionary[keyis] = list2 

with this:

dictionary[keyis] = [int(i) for i in list2]

You're creating list2 correctly, but when you go into the loop Python doesn't make a brand new copy of it with every iteration. It makes a pointer to the original list. Python sees:

dictionary[keyis] = list2

and says "oh, list2? I recognize that name! I have that as an object in memory already! I'll save some space by just updating the original copy and linking it here! Any time someone wants to view it or update it I'll just deal with the original and everything will be awesome forever!!!"

OK, so maybe the python interpreter isn't that enthusiastic, but that's how I like to think of it. The end result is that all of your dictionary values are pointing at the original list.

ate50eggs
  • 444
  • 3
  • 14
0

If you don't mind deleting every occurrence of 2 in the list, you can use list comprehension:

dictionary["00"] = [i for i in dictionary["00"] if i != 2]

This will create a new list, and will avoid altering the other values, as it appears all your dictionary values reference the same list.

EDIT: Yep your dictionary values reference the same list

you could use dictionary and list comprehension to create your dictionary

dictionary = {str(x):[i for i in range(10)] for x in range(100)}
user3636636
  • 2,409
  • 2
  • 16
  • 31