197

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

1
  • I think it's worth going down to @VladBezden's answer, which seems to be the most complete one to me.
    – Olivier
    May 23, 2022 at 0:44

12 Answers 12

185

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.

105

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]
6
  • 1
    I found this actually remove the item x+1, still useful though, thanks
    – Jack TC
    Feb 26, 2015 at 15:37
  • 7
    @JackTC it shouldn't Oct 25, 2020 at 19:58
  • 2
    Python seems to be somehow outdated in case of basic operations
    – Qbik
    Apr 8, 2021 at 7:00
  • exc = lambda s, i: s[:i] + s[i+1:] exc(mylist, 3) [0, 1, 2, 4, 5]
    – jfaleiro
    Apr 22, 2021 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
    – Gustin
    Mar 9, 2022 at 1:23
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

2
  • 2
    Good answer for a list. You could use it for arrays too but you'd need to use numpy.concatenate.
    – Bi Rico
    Oct 10, 2013 at 5:30
  • It doesn't work for range in python3
    – Uri Cohen
    Dec 2, 2022 at 14:24
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]
3
  • 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, 2013 at 3:53
  • @DSM: Please post this as an answer :-). Any case, I am not quite conversant with Numpy.
    – Abhijit
    Oct 10, 2013 at 3: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, 2013 at 5:30
19

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]])
0
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]
    
1
  • Ah! filterfalse is a great solution here (and my vote for the best non-numpy solution). Thanks!
    – pan0ramic
    Mar 18, 2022 at 23:37
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)
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
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
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!

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.