-2

EG.

list1 = [1, 2, 3, 4]

list1[0] = None 

How do I get the list to now start at 2 (make the index of 2 = list1[0]) because when I call list1[0] after doing this I get a blank output.

ddejohn
  • 8,775
  • 3
  • 17
  • 30

3 Answers3

1

Here' how to remove the first item using slicing

>>> list1 = [1, 2, 3, 4]
>>> list1 = list1[1:]
>>> list1[0]
2

Or you can use list.pop(0)

>>> item = list1.pop(0)
>>> item
2
>>> list1[0]
3
mhawke
  • 84,695
  • 9
  • 117
  • 138
  • I'd just like to add a link for an explenation about the notation from another post https://stackoverflow.com/questions/509211/understanding-slice-notation – Vulpex Mar 04 '21 at 04:12
  • [small benchmark](https://tio.run/##PY3NCsIwEITvfYq5lCRSSmtBpNAnKUUiphowPyTrwaePSQPOaWaYb9d/6eXsdPUhpT04A9JGaYI23gVCUF5JaprdBdygLYK0T8UnMTfIKnUsNZNYINdx3lgH9lDvHIbDy947zwdx@HUetzzkglW@iHJhtOX1FY9dPZZxnDAOWRm1H3NXYTmiEH/WB22Js/bSn3eYCIYWnAqnJtEh1mVdiZR@) – Manuel Mar 04 '21 at 04:29
0
print(list1)  # [1, 2, 3, 4]
list1.pop(0)
print(list1)  # [2, 3, 4]
print(list1[0])

=== Will print the following output

[1, 2, 3, 4]
[2, 3, 4]
2
ddejohn
  • 8,775
  • 3
  • 17
  • 30
-1

Otherwise you can del(index) or pop(index) a item with a certain index. Del just deletes the item from the list, pop deletes the item from the list and returns it. Have a look here: Difference between del, remove, and pop on lists