List indexing and splitting


  • The indexing is processed in the same way as it happens with the strings. The elements of the list can be accessed by using the slice operator [].
  • The index starts from 0 and goes to length - 1. The first element of the list is stored at the 0th index, the second element of the list is stored at the 1st index, and so on.

list_varible(start:stop:step)  
  • The start denotes the starting index position of the list.
  • The stop denotes the last index position of the list.
  • The step is used to skip the nth element within a start:stop
=======================================================================
Program

#example for List Indexing

list=[1,2,3,4,5,6,7]

print("listing of [0] is:", list[0])
print("                                                                                                      ")
print("listing of [1] is:", list[1])
print("                                                                                                      ")
print("listing of [2] is:", list[2]) 
print("                                                                                                      ")
print("listing of [3] is:", list[3])
print("                                                                                                      ")

 # Slicing the elements  
print("listing of 1 to 6:", list[0:6])  
print("                                                                                                      ")
# By default the index value is 0 so its starts from the 0th element and go for index -1.
print("list index:",list[:])
print("                                                                                                      ")
 
print("list index: 2 to 5",list[2:5]) 
print("                                                                                                      ")
print("list index: 1 to 6, sequence of 2 is ",list[1:6:2]) 
print("                                                                                                      ") 


OUTPUT:

listing of [0] is: 1
                                                                                                      
listing of [1] is: 2
                                                                                                      
listing of [2] is: 3
                                                                                                      
listing of [3] is: 4
                                                                                                      
listing of 1 to 6: [1, 2, 3, 4, 5, 6]
                                                                                                      
list index: [1, 2, 3, 4, 5, 6, 7]
                                                                                                      
list index: 2 to 5 [3, 4, 5]
                                                                                                      
list index: 1 to 6, sequence of 2 is  [2, 4, 6]