-1

If I have the list [0, 1, 2, 3, 4, 5] I can return the last four items using list[1:].

Is there a similar way of doing this that would return the list without the second item?
I.e. list[??] == [0, 2, 3, 4 ,5]

(If there are different methods for Python 2.x and Python 3.x please detail both)

2 Answers2

1

You can add two slices of the list

newLs = ls[:1] + ls[2:]

[0, 2, 3, 4, 5]

You can also delete the element

del ls[1]
DJK
  • 8,924
  • 4
  • 24
  • 40
0

you can try this:

n = 1

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

new_l = [a for i, a in enumerate(l) if i != n]
print(new_l)

Output:

[0, 2, 3, 4, 5]
Ajax1234
  • 69,937
  • 8
  • 61
  • 102