0

I have a following list:-

a = [1, 2, 3, 4]

From this list, I wanted to exclude the third item. How can i do this in python3.

For example in R, if a was a vector then, I can do the following:-

a <- c(1, 2, 3, 4)
a[-3]

But I can't seem to find how I can do this in python.

Edit: I don't want to delete the item. For example, if I used the following:-

del a[2]

That would actually remove the second element. Hence, if I did print(a), only three numbers would be printed.

What I want to know is how to exclude the third element but not delete it. Like in the R's example I have provided, it does not deletes the element, but rather excludes it. If in that example I did a[-3], only three items will be printed. But that does not meant the third item was deleted from the list.

Shawn Brar
  • 1,346
  • 3
  • 17

2 Answers2

0

This selects the third item from your list and removes it. It basically extracts the item on given index of the list. Indexing in Python starts at zero, so third item is on index 2.

a.pop(2)
chatax
  • 990
  • 3
  • 17
0

If you want to remove an element based on the index, you can use del. In python, index is 0 based.

a = [1,2,3,4]
del a[3]
a
[1,2,3]

this would remove the 4 element from the list.

Rajat Mishra
  • 3,635
  • 4
  • 27
  • 41