Level II

Access info inside a list

Alright, you know that lists exist. But…What is the point of having a list without accessing the values? Exactly! This can be done with “indices”. Indices access your data structure, in this case a list, by position from 0 to the end.

Remember

In Python, indexing starts at 0. That means that the first element in the list gets index 0, second element gets index 1, and so on until reaching the last element of the list, which has index equal to the length of the list minus 1.

Example:

phone_number= [1,2,3,4,5]

Number 1 corresponds to index 0 of the list, number 2 to index 1, number 3 to index 2, and so on.

You can check which elements are in each position (or index) of the list this way:

As you have seen in the last example, you can also access elements backward, by using negative elements. This way, index [-1] corresponds to the last element of the list, and [-2] to the penultimate element of the list. This is because the positions start with 0, this means that the last digit is the total length of the list (the amount of elements you have inside the list) – 1. Makes sense!

Interestingly, that’s not all, you can also access more than one value at a time! What if I want you to tell me the first three digits? You don’t need to do it one by one, you can do it with the slicing notation as follows: [position where to start : position where to end + 1]. Let’s see it in the example:

So, [0:3] will return you 3-0 = 3 elements, that is the elements with index 0, index 1 and index 2. Similarly, [4:8] will return you 8-4 = 4 elements, that is the elements with index 4, index 5, index 6 and index 7.

Also, if the first element in the slicing notation is 0, you don’t need to specify it; instead of [0:3], you can simply type [:3]. It works in the same way if the last element in the slicing notation is equal to the length of the list (the last element of the original list), you can type instead [5:], and this would return you all elements from index 5 until the end of the list.

Remember

When using the slicing notation [x:y] the elements in index y will NOT be included in the resulting sublist.

After everything you’ve learn, you may be wondering whether, having a list of list, you can access the internal lists and there elements too: yes, you can do that!

Here we show you how:

If you have paid attention to the last example, colors[0][0][0], you will have noticed that it returns the first character of the first element in the first list. That’s right! Characters in strings work as elements in lists, they also have an index and can also be accessed using the square brackets [ ] notation, you can even slice them:

Summary

  1. As you saw before, indices require to be between square braces [ ].
  2. You can access the positions in your list from 0 to the end of the list. Where 0 is the first position, and the last one is the length of the list -1. 
  3. You can access more than one position at once. It requires the following notation [position where the elements start:number of elements I want]
  4. If you have a list of multiple lists, which contain more lists, you can still access everything with [position].
  5. If you look for a position that doesn’t exist (i.e.= > the length of the list) there will be an error!