remove nth element from list python code example
Example 1: how to remove an element in a list by index python
list = [0, 1, 2, 3, 4, 5]
print(list)
del list[0]
print(list)
del list[-2]
print(list)
del list[0:2]
print(list)
Example 2: python list remove at index
>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> del a[-1]
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8]
Example 3: drop an intex in a list python
listOfnum.remove()
Example 4: python remove nth element from list
def drop_nth_element_in_list(_list : list, drop_step: int):
result_list = _list.copy()
counter = 0
if drop_step <= 1:
return []
for element in _list:
counter += 1
if counter == drop_step:
result_list.remove(element)
counter = 0
return result_list