I would like to comment more on the "afidelino" post. Along with that I am trying to explain in bit deep, taking same example. I am hoping that will help you to understand more. Pay attention to the fact that the indices of a sequence starting from scratch, and then to extract the second element must use the index 1. This has nothing to do with its content, which is confusing in the example 'two. Lists under the rules of the sequence (slice) that we have seen with strings. So for example we can take the last two elements:
Code:
>>> X [1:3]
['Two', 3.0]
>>> X [-2:]
['Two', 3.0]
Similarly we can get the length of the list using function len (). However, unlike strings, lists are mutable. We can then replace an element simply by assigning the value:
Code:
>>> X [1] = 2L
>>> X
[1, 2L, 3.0]
What is also true for the sequence below:
Code:
>>> X [-2:] = ['two', 3, 4.0]
>>> X
[1, 'two', 3, 4.0]
For example we can see that a slice can be assigned and replaced by another list, which may also have different lengths.
Bookmarks