![]() |
Working with dictionaries and keys - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Working with dictionaries and keys (/thread-3535.html) |
Working with dictionaries and keys - netrate - Jun-01-2017 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 RE: Working with dictionaries and keys - micseydel - Jun-01-2017 And your code? RE: Working with dictionaries and keys - netrate - Jun-01-2017 at school unfortunately. RE: Working with dictionaries and keys - micseydel - Jun-01-2017 Well when you get to school, please post your code since it's virtually impossible to help without it. RE: Working with dictionaries and keys - netrate - Jun-01-2017 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) RE: Working with dictionaries and keys - buran - Jun-01-2017 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. RE: Working with dictionaries and keys - buran - Jun-01-2017 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. RE: Working with dictionaries and keys - volcano63 - Jun-01-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_
RE: Working with dictionaries and keys - Larz60+ - Jun-01-2017 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: when you delete an item, all will be in new positionsso the about code will show n as changed. RE: Working with dictionaries and keys - wavic - Jun-01-2017 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'} |