Index all *except* one item in python

PythonListNumpyIndexing

Python Problem Overview


Is there a simple way to index all elements of a list (or array, or whatever) except for a particular index? E.g.,

  • mylist[3] will return the item in position 3

  • milist[~3] will return the whole list except for 3

Python Solutions


Solution 1 - Python

For a list, you could use a list comp. For example, to make b a copy of a without the 3rd element:

a = range(10)[::-1]                       # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
b = [x for i,x in enumerate(a) if i!=3]   # [9, 8, 7, 5, 4, 3, 2, 1, 0]

This is very general, and can be used with all iterables, including numpy arrays. If you replace [] with (), b will be an iterator instead of a list.

Or you could do this in-place with pop:

a = range(10)[::-1]     # a = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
a.pop(3)                # a = [9, 8, 7, 5, 4, 3, 2, 1, 0]

In numpy you could do this with a boolean indexing:

a = np.arange(9, -1, -1)     # a = array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
b = a[np.arange(len(a))!=3]  # b = array([9, 8, 7, 5, 4, 3, 2, 1, 0])

which will, in general, be much faster than the list comprehension listed above.

Solution 2 - Python

The simplest way I found was:

mylist[:x] + mylist[x+1:]

that will produce your mylist without the element at index x.

Example
mylist = [0, 1, 2, 3, 4, 5]
x = 3
mylist[:x] + mylist[x+1:]

Result produced

mylist = [0, 1, 2, 4, 5]

Solution 3 - Python

>>> l = range(1,10)
>>> l
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> l[:2] 
[1, 2]
>>> l[3:]
[4, 5, 6, 7, 8, 9]
>>> l[:2] + l[3:]
[1, 2, 4, 5, 6, 7, 8, 9]
>>> 

See also

https://stackoverflow.com/questions/509211/pythons-slice-notation

Solution 4 - Python

If you are using numpy, the closest, I can think of is using a mask

>>> import numpy as np
>>> arr = np.arange(1,10)
>>> mask = np.ones(arr.shape,dtype=bool)
>>> mask[5]=0
>>> arr[mask]
array([1, 2, 3, 4, 5, 7, 8, 9])

Something similar can be achieved using itertools without numpy

>>> from itertools import compress
>>> arr = range(1,10)
>>> mask = [1]*len(arr)
>>> mask[5]=0
>>> list(compress(arr,mask))
[1, 2, 3, 4, 5, 7, 8, 9]

Solution 5 - Python

Use np.delete ! It does not actually delete anything inplace

In your example, "mylist[~3]" would be written like that: mylist.delete(3)

A more complex example:

import numpy as np
a = np.array([[1,4],[5,7],[3,1]])                                       
 
# a: array([[1, 4],
#           [5, 7],
#           [3, 1]])

ind = np.array([0,1])                                                   

# ind: array([0, 1])

# a[ind]: array([[1, 4],
#                [5, 7]])

all_except_index = np.delete(a, ind, axis=0)                                              
# all_except_index: array([[3, 1]])

# a: (still the same): array([[1, 4],
#                             [5, 7],
#                             [3, 1]])

Solution 6 - Python

I'm going to provide a functional (immutable) way of doing it.

  1. The standard and easy way of doing it is to use slicing:

     index_to_remove = 3
     data = [*range(5)]
     new_data = data[:index_to_remove] + data[index_to_remove + 1:]
     
     print(f"data: {data}, new_data: {new_data}")
    

    Output:

     data: [0, 1, 2, 3, 4], new_data: [0, 1, 2, 4]
    
  2. Use list comprehension:

     data = [*range(5)]
     new_data = [v for i, v in enumerate(data) if i != index_to_remove]
    
     print(f"data: {data}, new_data: {new_data}") 
    

    Output:

     data: [0, 1, 2, 3, 4], new_data: [0, 1, 2, 4]
    
  3. Use filter function:

     index_to_remove = 3
     data = [*range(5)]
     new_data = [*filter(lambda i: i != index_to_remove, data)]
    

    Output:

     data: [0, 1, 2, 3, 4], new_data: [0, 1, 2, 4]
    
  4. Using masking. Masking is provided by itertools.compress function in the standard library:

     from itertools import compress
     
     index_to_remove = 3
     data = [*range(5)]
     mask = [1] * len(data)
     mask[index_to_remove] = 0
     new_data = [*compress(data, mask)]
    
     print(f"data: {data}, mask: {mask}, new_data: {new_data}")
    

    Output:

     data: [0, 1, 2, 3, 4], mask: [1, 1, 1, 0, 1], new_data: [0, 1, 2, 4]
    
  5. Use itertools.filterfalse function from Python standard library

     from itertools import filterfalse
     
     index_to_remove = 3
     data = [*range(5)]
     new_data = [*filterfalse(lambda i: i == index_to_remove, data)]
     
     print(f"data: {data}, new_data: {new_data}")
    

    Output:

     data: [0, 1, 2, 3, 4], new_data: [0, 1, 2, 4]
    

Solution 7 - Python

If you don't know the index beforehand here is a function that will work

def reverse_index(l, index):
    try:
        l.pop(index)
        return l
    except IndexError:
        return False

Solution 8 - Python

Note that if variable is list of lists, some approaches would fail. For example:

v1 = [[range(3)] for x in range(4)]
v2 = v1[:3]+v1[4:] # this fails
v2

For the general case, use

removed_index = 1
v1 = [[range(3)] for x in range(4)]
v2 = [x for i,x in enumerate(v1) if x!=removed_index]
v2

Solution 9 - Python

If you want to cut out the last or the first do this:

list = ["This", "is", "a", "list"]
listnolast = list[:-1]
listnofirst = list[1:]

If you change 1 to 2 the first 2 characters will be removed not the second. Hope this still helps!

Solution 10 - Python

arr=[1,3,5,7,9]
for i in range(len(arr)):
    arsum = arr[:i] + arr[i + 1:]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncholdgrafView Question on Stackoverflow
Solution 1 - Pythontom10View Answer on Stackoverflow
Solution 2 - PythonAndre SoaresView Answer on Stackoverflow
Solution 3 - PythonAndreas JungView Answer on Stackoverflow
Solution 4 - PythonAbhijitView Answer on Stackoverflow
Solution 5 - PythonbrnlView Answer on Stackoverflow
Solution 6 - PythonVlad BezdenView Answer on Stackoverflow
Solution 7 - PythonabergerView Answer on Stackoverflow
Solution 8 - Pythonshahar_mView Answer on Stackoverflow
Solution 9 - Pythonchboo1View Answer on Stackoverflow
Solution 10 - PythonShyam GuptaView Answer on Stackoverflow