How to delete multiple specified positions (indexes) in a Python list

If you want to delete multiple specified positions (indexes), you cannot use pop or del. (Please make it usable ...)

l = ['0', '1', '2', '3', '4']

del l[0,1,3]
# TypeError: list indices must be integers or slices, not tuple

l.pop(0,1,3)
# TypeError: pop() takes at most 1 argument (3 given)

Instead of these, there is a list comprehension notation as a simple way to describe.


l = [x for i, x in enumerate(l) if i not in [0,1,3] ]

print(l)
# ['2', '4']

Recommended Posts

How to delete multiple specified positions (indexes) in a Python list
How to clear tuples in a list (Python)
Delete multiple elements in python list
How to slice a block multiple array from a multiple array in Python
How to define multiple variables in a python for statement
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 list files under the specified directory in a list (multiple conditions / subdirectory search)
[Python] How to convert a 2D list to a 1D list
How to get a stacktrace in python
How to create a JSON file in Python
How to notify a Discord channel in Python
[Python] How to draw a histogram in Matplotlib
Sort list elements in a specified order in Python
How to remove duplicate elements in Python3 list
[Python] How to put any number of standard inputs in a list
[Introduction to Python] How to delete rows that meet multiple conditions in Pandas.DataFrame
How to write a string when there are multiple lines in python
How to format a list of dictionaries (or instances) well in Python
How to pass the execution result of a shell command in a list in Python
How to convert / restore a string with [] in python
How to write string concatenation in multiple lines in Python
[Python] How to expand variables in a character string
How to write a list / dictionary type of Python3
Specify multiple list indexes (Python)
Things to note when initializing a list in Python
[Python] How to sort dict in list and instance in list
How to retrieve multiple arrays using slice in python.
How to execute a command using subprocess in Python
[Python] How to output the list values in order
How to get a list of files in the same directory with python
How to identify the element with the smallest number of characters in a Python list?
How to check in Python if one of the elements of a list is in another list
How to find the first element that matches your criteria in a Python 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)
A story about how to specify a relative path in python.
How to use the __call__ method in a Python class
How to import a file anywhere you like in Python
A simple way to avoid multiple for loops in Python
Developed a library to get Kindle collection list in Python
I tried "How to get a method decorated in Python"
How to develop in a virtual environment of Python [Memo]
Extract multiple list duplicates in Python
How to write a Python class
[Python] How to do PCA in Python
[python] Manage functions in a list
How to collect images in Python
How to use SQLite in Python
[Python] How to use list 3 Added
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
How to delete a Docker container
How to handle Japanese in Python
How to pass the execution result of a shell command in a list in Python (non-blocking version)
How to display a specified column of files in Linux (awk)
How to determine the existence of a selenium element in Python
How to pass arguments to a Python script in SPSS Modeler Batch
Try to get a list of breaking news threads in Python.