Posts: 6
Threads: 4
Joined: Nov 2016
I have a dictionary with a 10 people, the key being a number (0-10) and the value being the people's name. I am in the processing of Insert, Adding and deleting from the dictionary. All seems well until I delete a person and add a new one. The numbers (keys) do not change and so I am getting an non-sequential set of numbers for the keys. Is there a way of performing this where the key will update so that is continues to work sequentially? Here is what I mean
Print dictionary
{0 John, 1 David, 2 Phil, 3 Bob}
remove 1 David
{0 John, 2 Phil, 3 Bob}
How can I get it so that the VALUE will reset and it will look like this after both adding or deleting?
{0 John, 1 Phil, 2 Bob}
my code here
Posts: 2,342
Threads: 62
Joined: Sep 2016
Posts: 6
Threads: 4
Joined: Nov 2016
Posts: 2,342
Threads: 62
Joined: Sep 2016
Well when you get to school, please post your code since it's virtually impossible to help without it.
Posts: 6
Threads: 4
Joined: Nov 2016
I just did some sample code to show what I am talking about, it is more comprehensive at school, but you will get the idea
dict = {0: "David", 1 : "bill", 2 :"seth"}
print (dict)
ask=0
while ask != 4:
ask=int(input("Do you want 1) add 2) delete 3) clear list, 4) exit"))
if ask == 1:
add=input("Who do you want to add?")
dict[len(dict)]=add
print(dict)
elif ask == 2:
newdel=int(input("which number do you want to delete?"))
del dict[newdel]
print(dict)
Posts: 8,155
Threads: 160
Joined: Sep 2016
What is the problem with the non-sequential keys?
Based on your description of desired functionality it sounds like you better use list, not dict. List indexes are sequential.
Posts: 8,155
Threads: 160
Joined: Sep 2016
Jun-01-2017, 02:45 AM
(This post was last modified: Jun-01-2017, 02:46 AM by buran.)
Also, dict is unordered, so actually there is no guarantee as to the order of keys, unless you use OrderedDict from collections. In python3.6 the order of the keys of dict are preserved but this is still considered implementation detail and should not be relied upon.
Posts: 566
Threads: 10
Joined: Apr 2017
(Jun-01-2017, 02:22 AM)netrate Wrote: dict = {0: "David", 1 : "bill", 2 :"seth"}
print (dict)
.......
You are shadowing Python standard function dict by overwriting its value. This is a bad practice. If you cant't come up with any name - my_dict , e.g. (not good in development, but OK for exercises) - add underscore at least, like dict_
Test everything in a Python shell (iPython, Azure Notebook, etc.) - Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
- Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
- You posted a claim that something you did not test works? Be prepared to eat your hat.
Posts: 12,024
Threads: 484
Joined: Sep 2016
A dictionary is a hash table.
This means that a key is assigned and remains static.
It should not be changed unless there is a good reason.
Also, since it is a hash table, the order that the key value pairs are stored in the dictionary is dependent on it's key hash
unless you are running python 3.6 which has ordered dictionaries by default.
It looks like you should be using a list, not a dictionary, in which case, you would just add the names:
mylist = ['John', 'David', 'Phil', 'Bob']
for n, name in enumerate(mylist):
print({}, {}'.format(n, name)) results:
Output: 0, John
1, David
2, Phil
3, Bob
when you delete an item, all will be in new positions
so the about code will show n as changed.
Posts: 2,953
Threads: 48
Joined: Sep 2016
More of that. The order of the dict in Python 3.6 fallow the order of inserting its elements.
So, if you delete a key and it's value and insert again the same key with a new value they will be positioned after all the others when printing it.
In [1]: dict_ = {num: num**2 for num in range(2,5)}
In [2]: dict_
Out[2]: {2: 4, 3: 9, 4: 16}
In [3]: dict_.pop(3)
Out[3]: 9
In [4]: dict_
Out[4]: {2: 4, 4: 16}
In [5]: dict_[3] = 3**3
In [6]: dict_ # here the order is preserved because of the interpreter. It's doing it
Out[6]: {2: 4, 3: 27, 4: 16}
In [7]: dict_[1] = 'one'
In [8]: dict_
Out[8]: {1: 'one', 2: 4, 3: 27, 4: 16} # the same
In [9]: print(dict_) # but
{2: 4, 4: 16, 3: 27, 1: 'one'}
|