Python 3.4 - Indexing and slicing

 

Indexing and slicing 

Indexing and slicing are two approaches, used to access or retrieve elements of a sequence datatype such as string, list, tuple etc.

Indexing

Indexing is used to access a single element at a time. We can use index number of an element of a sequence to access that particular element. Every element in a sequence such as string, list, tuple, bytes etc. has an index number. An index number starts from 0 (zero) in Python. So an index number is used to access an element. For example, in a string “india” the index number of first element ‘i’ is 0, the index number of second element ‘n’ is 1, the index number of third element ‘d’ is 2 and so on. The index number of last element ‘a’ is 4. So remember that, the indexing starts from 0 and ends at n-1 in a sequence of n length. In our case, string “india” has length 5 i.e. 5 characters or elements. So the indexing started from 0 and ended at 4 (n-1 or 5-1). Same concept applies to all the other sequences. See the image below – 


 

We can access every element with following syntax 

Sequence-name[index value] 

For example, we have a list named ‘lst’ - 

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

Now we can access any element of the list like this –

lst[1] # will access 2nd element (1st index) i.e. 2

lst[3] # will access 4th element (3rd index) i.e. 4

If we try to access the element whose index value is out of the range, then we will get an error message “index out of range”. For example, list ‘lst’ has indexes from 0 to 4 and if we try to access the element at index 5 then it will give an error since there is no element at that index.

lst[5] # will give an error

We can also access the elements in reverse order. In reverse order the index value of the last element will be -1. The 2nd last element will have index value -2 and so on. In above example if we move reverse then, the element 5 has index -1, element 4 has index -2 and so on. The element 1 will have index -5. Remember that indexing in reverse order begins with -1 not 0. So we can also access elements using minus indexes such as- 

lst[-2] # will give 4

lst[-5] # will give 1

lst[-1] # will give 5

lst[-6] # will give error

let’s write a program to understand indexing –

 

# Python program to understand indexing

lst = [1, 2, 3, 4, 5, 6, 7] # lst is a list

str = "easy" # str is a string

tpl = (8, 9, 10) # tpl is a tuple

# Now we are going to access elements using index value

print("1st element of lst from beginning =",lst[0])

print("5th element of lst from beginning =",lst[4])

print("2nd element of lst from reverse =",lst[-2])

print("3rd element of str from beginning =",str[2])

print("4th element of str from reverse =",str[-4])

print("2nd element of tpl from beginning =",tpl[1])

print("3rd element of tpl from reverse =",tpl[-3])

 

Output –

 

1st element of lst from beginning = 1

5th element of lst from beginning = 5

2nd element of lst from reverse = 6

3rd element of str from beginning = s

4th element of str from reverse = e

2nd element of tpl from beginning = 9

3rd element of tpl from reverse = 8

Slicing

Indexing is used to access a single element of a sequence at a time whereas slicing is used to access a slice or a part of sequence such as list, string or tuple. We also need index numbers for slicing operations. The syntax to use slicing is –

Sequence-name[start : stop : step]

We already know what start, stop and step are. If not, then go check range Datatype. Now let’s take an example to extract slices from sequences –

lst = [1,2,3,4,5,6,7,8,9,10]

print(lst[0:5]) # prints [1,2,3,4,5] , from index 0 to 4

print(lst[4:9]) # prints [5,6,7,8,9] , from index 4 to 8

print(lst[0:15]) # prints [1,2,3,4,5,6,7,8,9,10] will print whole list

print(lst[1:5:2]) # prints [2,4] , form index 1 to 4 with a step value of 2

Please note that, if we write only one value without colon then it will become indexing not slicing. For example, if we write – 

print(lst[1]) # prints 2 , it is indexing 

We can also use slicing in following way where some parts are missing – 

print(lst[1:]) # prints whole list from index 1 i.e. [2,3,4,5,6,7,8,9,10]

print(lst[: : -1]) # prints reverse list [10,9,8,7,6,5,4,3,2,1]

print(lst[ :5: ]) # prints [1,2,3,4,5]

Important – When start is missing then 0 is default value, when step is missing then 1 is default value and when stop is missing then n is default value where n is the length of sequence. We can also traverse from back with negative step value. I recommend, you practice a lot with slicing.


Comments

Post a Comment

Popular posts from this blog

Python 4.4 - Points to remember

Python 3.6 - Points to remember

Python 3.3 - Datatypes in Python