List Manipulation

Total Post:113

Points:791
 130  View(s)
Ratings:
Rate this:

Explain the list manipulation in python with example ?

  1. Post:140

    Points:984
    Re: List Manipulation

    List manipulation in Python :

    List is one of the simplest data structures in Python.Its very important and Lists are enclosed in square brackets [ ]. each item is separated by a comma.Every List item assign unique index no. A list is mutable, meaning you can change its contents. Lists have many built-in control functions. 

    Methods of List objects are: 

    Creation: 

    L = ['yellow', 'red', 'blue', 'green', 'black'] 

    print L :

    ['yellow', 'red', 'blue', 'green', 'black'] 

    Accessing/Indexing : 

    L[0]  = 'yellow' 

    Slicing: 

    L[1:4]  = ['red', 'blue', 'green'] 
    L[2:]   = ['blue', 'green', 'black']
    
    L[:2] =  ['yellow', 'red']
    L[-1] =  'black'
    L[1:-1] = ['red', 'blue', 'green']

    Lengthof items in list :

    len(L)  =  5 

    Sorting the list :

    sorted(L) = ['black', 'blue', 'green', 'red', 'yellow'] 

    Append to end of list: 

    L.append("pink")

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!