To remove an element from the beginning of an array (or list) in Python, you can use slicing. Slicing is a way to extract a subset of elements from an array by specifying a start and end index.
To remove the first element of an array, you can specify a slice that starts from the second element (index 1) and ends at the last element using the colon operator :
.
Here's an example:
main.py91 chars4 lines
In this example, we create an array my_array
with 5 elements. We then use slicing to create a new array that starts from the second element (my_array[1]
) and ends at the last element (my_array[-1]
). Finally, we assign the new array back to my_array
to replace the original array. The output shows that the first element has been removed from the array.
gistlibby LogSnag