9

Being that the key has multiple values and I want to remove the one that is the same as the key itself? That is, I have a dictionary jumps:

jumps = {'I6': ['H6', 'I6', 'I5'], 'T8' : ['T6', 'S6', 'T8']}

And I want to delete the value 'I6' from the 'I6' key and also 'T8' from the 'T8' key. How could I do this? I'm getting mixed up in parsing the strings versus the values.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
Cora Coleman
  • 237
  • 1
  • 5
  • 14
  • The answer can be found from [here](http://stackoverflow.com/questions/5844672/delete-an-item-from-a-dictionary). This is duplicate question. – kingsj0405 Mar 20 '17 at 01:33
  • 3
    @bismute: in that answer an **entire** key and its corresponding value is deleted, here the values are "manipulated". – Willem Van Onsem Mar 20 '17 at 01:33

5 Answers5

10

You can use a one-liner with both dictionary comprehension and list comprehension:

result = {k:[vi for vi in v if k != vi] for k,v in jumps.items()}

This results in:

>>> {k:[vi for vi in v if k != vi] for k,v in jumps.items()}
{'T8': ['T6', 'S6'], 'I6': ['H6', 'I5']}

Note that you will remove all elements from the lists that are equal to the key. Furthermore the remove process is done for all keys.

The code works as follows: we iterate over every key-value pair k,v in the jumps dictionary. Then for every such pair, we construct a key in the resulting dictionary, and associate [vi for vi in v if k != vi] with it. That is a list comprehension where we filter out all values of v that are equal to k. So only the vis remain (in that order) that are k != vi.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • 1
    This makes so much sense and works perfectly, and moreover, thank you for your helpful explanation! I will mark this as the answer when I am allowed to! – Cora Coleman Mar 20 '17 at 01:40
6

There is a built in command, called remove that will remove an item from a list. We can start by accessing the element from our dictionary by using a string key. That value, happens to be a list, that we can then use the remove command on.

Here's your list to demonstrate:

jumps = {
    'I6': [ # we are accessing this value that happens to be a list
        'H6',
        'I6', #then Python will sort for and remove this value
        'I5'
    ],
    'T8' : [
        'T6',
        'S6',
        'T8'
    ]
}



jumps = {'I6': ['H6', 'I6', 'I5'], 'T8' : ['T6', 'S6', 'T8']}

jumps['I6'].remove('I6')
jumps['T8'].remove('T8')
print(jumps)
Neil
  • 14,063
  • 3
  • 30
  • 51
5
for key in jumps:
    jumps[key].remove(key)
Stefan Pochmann
  • 27,593
  • 8
  • 44
  • 107
0

Works for me

jumps = {'I6': ['H6', 'I6', 'I5'], 'T8' : ['T6', 'S6', 'T8']}

key = 'I6'

if key in jumps[key]:
    jumps[key].remove(key)

print(jumps)
user3750325
  • 1,502
  • 1
  • 18
  • 37
0

The other possible way might be:

{vals.remove(val) for key,vals in jumps.items() for val in vals if val == key}

Since, it is referencing to the values list or array, the removal from list will affect in values of jumps.

niraj
  • 17,498
  • 4
  • 33
  • 48
  • After the call, it would change the `jumps` though. For the time complexity it may not be efficient though, however wouldn't it be space efficient since we are not using anything except `jumps` ( `{None}` would be extra space)? – niraj Mar 20 '17 at 06:00