Reputation: 186
I am creating a dictionary, that potentially has keys that are the same, but the values of each key are different. Here is the example dict:
y = {44:0, 55.4:1, 44:2, 5656:3}
del y[44]
print y
{5656: 3, 55.399999999999999: 1}
I would like to be able to do something like:
del y[44:0]
Or something of that nature.
Upvotes: 0
Views: 170
Reputation: 22465
Before deleting:--
>>>dic={"a":"A","b":"B","c":"C","d":"D","e":"E"}
>>> del dic['a']
after deleting :-
>>>dic
{'b': 'B','c': 'C','d': 'D','e': 'E'}
Upvotes: -1
Reputation: 1212
Just an idea- instead of having scalar values, why not a collection of some kind? Perhaps even a set if they are unique values:
myDict = {44:set([1, 2, 3])}
So to add or remove an object:
myDict[44].add(1)
myDict[44].remove(1)
For adding a new key:
if newKey not in myDict:
myDict[newKey] = set() # new empty set
Upvotes: 1
Reputation: 1802
Your question is moot. In your y
declaration, the 44:2
wouldn't go alongside 44:0
, it would overwrite it. You'd need to use a different key if you want both values in the dictionary.
Upvotes: 0
Reputation: 281835
You never had duplicate keys:
>>> y = {44:0, 55.4:1, 44:2, 5656:3}
>>> y
{5656: 3, 55.4: 1, 44: 2}
A dictionary can be initialised with duplicate keys, but all but one will be ignored.
Upvotes: 4