Index all *except* one item in python
--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Peaceful Mind
--
Chapters
00:00 Index All *Except* One Item In Python
00:23 Answer 1 Score 60
00:37 Answer 2 Score 33
01:00 Accepted Answer Score 191
01:46 Answer 4 Score 112
02:03 Thank you
--
Full question
https://stackoverflow.com/questions/1928...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #list #numpy #indexing
#avk47
    Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Peaceful Mind
--
Chapters
00:00 Index All *Except* One Item In Python
00:23 Answer 1 Score 60
00:37 Answer 2 Score 33
01:00 Accepted Answer Score 191
01:46 Answer 4 Score 112
02:03 Thank you
--
Full question
https://stackoverflow.com/questions/1928...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #list #numpy #indexing
#avk47
ACCEPTED ANSWER
Score 191
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.
ANSWER 2
Score 112
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]
ANSWER 3
Score 60
>>> 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
ANSWER 4
Score 33
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]