3

Here we go again. So what I am trying to do is, let's say I have a list like this:

List = ['one','two','three']

And they are all assigned to numbers 0,1,2 for example:

'one': 0, 'two': 1, 'three': 2

What I want to do now is I want to remove an item from my list... let's say two

List.remove('two')

But if I do this, 'three' goes into the place of two and the total amount decreases by one. Is there a way I can remove two, but still keep the total amount of objects in a list, with the ones being removed just being replaced with 'None' or something similar?

2 Answers2

7

You can replace it with None or with an empty string '' using:

List[List.index('two')] = None
# List = ['one', None, 'three']

or

List[List.index('two')] = ''
# List = ['one', '', 'three']
James
  • 32,991
  • 4
  • 47
  • 70
0

You can iterate over the dictionary and replace the value that you want to remove:

s = {'one': 0, 'two': 1, 'three': 2}
target = "two"
new_s = {a if a != target else None:b for a, b in s.items()}

Output:

{'one': 0, 'three': 2, None: 1}

This way, the count value for "two" is retained, but the key substituted with None

Ajax1234
  • 69,937
  • 8
  • 61
  • 102
  • `What I want to do now is I want to remove an item from my list` I think OP wants to remove the item from a list and leave the dictionary (which I assume is just a look up for where the key should be if it exist) alone. – MooingRawr Nov 15 '17 at 14:37