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?
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].
len(list) returns 5, then you have 4 elements in your list"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
__len__;)