1

Let say I have a list with len(list) == 5

Can I be sure list[4] exists?

Or maybe an item could get deleted and then the list would have indices 0,1,2,3,5 which would have length 5 but without index 4?

3
  • Lists starts with index 0, so the last index always will be len(list)-1 Commented Mar 21, 2018 at 16:01
  • 1
    @vaultah actually no, they could override __len__ ;) Commented Mar 21, 2018 at 16:10
  • Take a look at this post: stackoverflow.com/questions/1857780/… Commented Mar 21, 2018 at 17:53

5 Answers 5

1

Can I be sure list[4] exists?

Yes you can. If len(list) returns 5, then you are 100% sure that there are 5 elements in your list.

Or maybe an item could get deleted and then the list would have indices 0,1,2,3,5 which would have length 5 but without index 4?

Again, if len(list) returns 5, then you have 5 elements in your list. And because lists are zero-based in python, then list[4] will be the last element of your list

Here is a quote from the python documentation:

Operation: s[i]

Result: ith item of s

Notes: origin 0

You can also try it in the REPL (python language shell), as Jim showed in his answer. If you create a list of 5 elements, len will return you 5. If you delete any element in the list, such as the 4th one, then len will now return you 4, and you will access the last element (which was the 5th element before the deletion) by using list[4].

Sign up to request clarification or add additional context in comments.

1 Comment

I think you meant 5 here: "Again, if len(list) returns 5, then you have 4 elements in your list"
0

When you delete an item at a specific index, the rest of the data will shift back to take that place.

>>> l = ['a', 'b', 'c', 'd', 'e', 'f']
>>> len(l)
6
>>> l[4]
'e'

>>> del l[4]
>>> len(l)
5
>>> l[4]
'f'

Comments

0

When an element from a list is deleted, the index's shift down one.

An index is just an element located in a list.

if you have a list of 1,2,3,4,5 and you delete list[3] then you now have 1,2,3,5 and now list[3] == 5

Comments

0

Yes, if len(lst) == x then all indices up to x - 1 exist.

Look at what happen if you delete an item.

lst = [1, 2, 3]

len(lst) # 3

del lst[1]

len(lst) # 2

After deletion, the length was decremented.

Comments

0

A list in Python is a list implemented with an array, it is not just an array. If you take some lessons on data structures you can learn more about this, but the consequence of this is that a deletion of an entry of a list will be resolved by changing all the indices of the items coming after the removed item.

Like this

>>> x = [0, 1, 2, 3]
>>> del x[2]
>>> x
[0, 2, 3]
>>> x[2]
3

2 Comments

you probably mean "is an array implementation, not a list implementation" right?
@Jean-FrançoisFabre CPython uses an ArrayList with pointers, simply said. So a list implemented with an array, yes. Don't really know how to put it in a simple manner.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.