194

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

Yamaneko
  • 3,433
  • 2
  • 38
  • 57
choldgraf
  • 3,539
  • 4
  • 22
  • 27
  • I think it's worth going down to [@VladBezden's answer](https://stackoverflow.com/a/60150767/7009806), which seems to be the most complete one to me. – Olivier May 23 '22 at 00:44

12 Answers12

184

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.

tom10
  • 67,082
  • 10
  • 127
  • 137
103

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]
Andre Soares
  • 1,968
  • 3
  • 21
  • 24
  • 1
    I found this actually remove the item x+1, still useful though, thanks – Jack TC Feb 26 '15 at 15:37
  • 7
    @JackTC it shouldn't – theonlygusti Oct 25 '20 at 19:58
  • 2
    Python seems to be somehow outdated in case of basic operations – Qbik Apr 08 '21 at 07:00
  • `exc = lambda s, i: s[:i] + s[i+1:]` `exc(mylist, 3)` [0, 1, 2, 4, 5] – jfaleiro Apr 22 '21 at 17:35
  • if `x==5`, it will **not** raise an IndexError. In Python, list slicing out of bounds does not result in an error, because when the start/end indices of the slice are greater than the length of the list, then they are simply reduced to the length of the list. Source: [Python Docs note 4](https://docs.python.org/3/library/stdtypes.html#sequence-types-list-tuple-range) – Gustin Mar 09 '22 at 01:23
  • This is 7x faster than the accepted answer. – pavi2410 Apr 27 '23 at 15:16
58
>>> 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

Explain Python's slice notation

Community
  • 1
  • 1
32

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]
Abhijit
  • 62,056
  • 18
  • 131
  • 204
  • 4
    I might use something like `np.arange(len(arr)) != 3` as the mask, because then it can be inlined, e.g. `arr[~(np.arange(len(arr)) == 3)]` or whatever. – DSM Oct 10 '13 at 03:53
  • @DSM: Please post this as an answer :-). Any case, I am not quite conversant with Numpy. – Abhijit Oct 10 '13 at 03:54
  • +1 for masking an array, in the case of a list I would go with slice and concatenate over using compress. – Bi Rico Oct 10 '13 at 05:30
18

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]])
brnl
  • 181
  • 1
  • 5
8

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]
    
Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179
1

For 1D numpy, np.concatenate is faster than np.arange method.

Benchmark:

x = np.arange(1000) * 2
i = 3

%timeit np.concatenate((x[:i],x[i+1:]))
%timeit x[np.arange(len(x))!=i]

# 9.21 µs ± 467 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
# 32.8 µs ± 7.46 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
Muhammad Yasirroni
  • 1,512
  • 12
  • 22
1
idxs = list(range(len(array))).remove(idx)
array[idxs]

This should work, it removes the not needed index and then slices the array

0

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
aberger
  • 2,299
  • 4
  • 17
  • 29
0

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
shahar_m
  • 3,461
  • 5
  • 41
  • 61
0

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!

chboo1
  • 63
  • 9
0
arr=[1,3,5,7,9]
for i in range(len(arr)):
    arsum = arr[:i] + arr[i + 1:]
Shyam Gupta
  • 489
  • 4
  • 8