Python Forum
Working with dictionaries and keys
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Working with dictionaries and keys
#1
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
Reply
#2
And your code?
Reply
#3
at school unfortunately.
Reply
#4
Well when you get to school, please post your code since it's virtually impossible to help without it.
Reply
#5
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)
Reply
#6
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.
Reply
#7
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.
Reply
#8
(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.
Reply
#9
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.
Reply
#10
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'}
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Question about working with dictionaries Ashcora 13 2,079 Dec-27-2022, 09:09 PM
Last Post: Ashcora
  Best way to support multiple keys in dictionaries mrapple2020 3 2,667 Apr-06-2019, 06:54 AM
Last Post: mrapple2020
  Working with files and dictionaries OmarSinno 1 2,610 Oct-30-2017, 11:02 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020