Python | Access List Items

python access list items,python access list element by index
python access list element by index

Python | Access List Items 

    List items ordered , changeable , and allows to duplicate items 

    List Items are stored indexing manner in square brackets so First item index start from 
    0,1,2..... so on


Example

    list = [ 'mango' ,' banana', 'apple' ]

    print(list[0])     

    print(list[1])


Output

    mango

    banana



Negative Indexing

    Negative indexing start counting from end of the list

    -1 is the last index of list and -2 second last


Example

    list = [ 'mango' ,' banana', 'apple' ]    

    print(list[-1])         


Output

     apple


Range of Indexing

    you can set the range of index by start and where to end the range

    In Range of Index , the start index is included and the end index is Excluded in runtime 

    the counting of index is start from Zero (0) 


Example

    list = [ 'mango' ,' banana', 'apple', 'kiwi' , 'orange', 'graps' , 'pineapple' ]    

    print(list[1:5])        


Output

    ' banana', 'apple', 'kiwi' , 'orange'


Now , we want only those items its index start from 2 to end in list


    list = [ 'mango' ,' banana', 'apple', 'kiwi' , 'orange', 'graps' , 'pineapple' ]

    

    print(list[1:])        

    print(list[:5])         


Output


    ' banana', 'apple', 'kiwi' , 'orange', 'graps' , 'pineapple'

    'mango' ,' banana', 'apple', 'kiwi' , 'orange'


Post a Comment (0)
Previous Post Next Post