Remove Element at Index

Say we have a list and we wanted to remove the element at index 2.

# Current array
array = ["1", 2, "three", 4, 5]

# What we want
# array = ["1", 2, 4, 5]

It’s a simple for-loop that moves the element at index + 1 till the end to the left, then pops the last element since it is duplicated.

# Create our array
array = ["1", 2, "three", 4, 5]

def ithElementRemove(arr, ele):
    for i in range(ele+1, len(arr)):
        arr[i-1] = arr[i]
    arr.pop()
    print(arr)


ithElementRemove(array, 2)
# Output: ["1", 2, 4, 5]
Previous
Previous

User AUTH in MERN

Next
Next

Fibonacci Number