[PYTHON] How to get a list excluding elements whose index is i ...?

I'm curious about the fastest way to get a list without * changes * to the list ʻitems * without ʻitems [i], or a reasonably fast and smart way to write (sometimes excluding the i-th in math). , Because I want something like that).

I measured it with % timeit of Jupyter.

If you don't use numpy

items = list(range(1000000))
i = 17 #suitable

Combine slices

%timeit items[:i] + items[i+1:]
11.2 ms ± 349 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

Included notation (using ʻenumerate`)

%timeit [item for k, item in enumerate(items) if k != i]
65.1 ms ± 212 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

Comprehension notation

%timeit [items[k] for k in range(len(items)) if k != i]
75.5 ms ± 800 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

It seems that slicing is overwhelmingly faster. By the way, I was a little surprised that using the second ʻenumerate` is faster than the third example.

When using numpy

@ antimon2's Maru Park, but there are the following methods.

import numpy as np
items = np.arange(1000000)
i = 17

np.hstack

%timeit np.hstack([items[:i], items[i+1:]])
584 µs ± 6.32 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

np.delete

%timeit np.delete(items, i)
585 µs ± 3.73 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

Boolean index

%timeit items[np.arange(items.size) != i]
1.9 ms ± 5.82 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

Comprehension notation

%timeit np.array([items[k] for k in range(len(items)) if k != i])
182 ms ± 3.85 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

Again, the slices combine very quickly. Considering readability, is np.delete the best because the speed does not change so much?

If you have any other good ones, please let me know.

Recommended Posts

How to get a list excluding elements whose index is i ...?
How to check in Python if one of the elements of a list is in another list
I tried "How to get a method decorated in Python"
How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
How to get a list of links from a page from wikipedia
I tried to get the index of the list using the enumerate function
[Python] How to convert a 2D list to a 1D list
How to get a stacktrace in python
I measured 6 methods to get the index of the maximum value (minimum value) of the list
How to clear tuples in a list (Python)
Get only the subclass elements in a list
I read "How to make a hacking lab"
I made a tool to get new articles
How to remove duplicate elements in Python3 list
How to get a list of files in the same directory with python
[Introduction to Python] How to get the index of data with a for statement
How to get the "name" of a field whose value is limited by the choice attribute in Django's model
[Python] I want to use only index when looping a list with a for statement
Convert a slice object to a list of index numbers
How to get a logged-in user with Django's forms.py
I want to get started with the Linux kernel, what is the list head structure?
How to write a list / dictionary type of Python3
[Python] I want to make a nested list a tuple
How to use list []
[Python] How to make a list of character strings character by character
How to shuffle a part of a Python list (at random.shuffle)
[Python] I want to get a common set between numpy
[Command] Command to get a list of files containing double-byte characters
Get the number of specific elements in a python list
Developed a library to get Kindle collection list in Python
Since Python 1.5 of Discord, I can't get a list of members
I tried to get started with Hy ・ Define a class
How to display a list of installable versions with pyenv
How to extract other than a specific index with Numpy
I wrote a script to get a popular site in Japan
How to get a quadratic array of squares in a spiral!
How to connect the contents of a list into a string
How to call a function
Python list is not a list
How to hack a terminal
It is surprisingly troublesome to get a list of the last login date and time of Workspaces
I tried to get a database of horse racing using Pandas
Try to get a list of breaking news threads in Python.
How to get all the possible values in a regular expression
A memorandum when I tried to get it automatically with selenium
How easy is it to synthesize a drug on the market?
I wrote a script to get you started with AtCoder fast!
I tried to create a list of prime numbers with python
[Python] How to get & change rows / columns / values from a table.
Here's a brief summary of how to get started with Django
How to use Visual Recognition to get LINE ID from a girl
I don't know how to get query parameters on GAE / P
How to get the vertex coordinates of a feature in ArcPy
[Python] How to get a value with a key other than value with Enum
Is R's do.call () a classical higher-order function? Learn how to use
I want to make a parameter list from CloudFormation code (yaml)
How to get a job as an engineer from your 30s
How to remove duplicates from a Python list while preserving order.
How to delete multiple specified positions (indexes) in a Python list
I did a lot of research on how Python is executed