List Operations in Python | Class 11 | Class 12 | Computer Science | Informatics Practices | Notes | Study Material | Python Revision Tour

    List Manipulation In Python     

    Operation On LISTS  

YouTube Video: List Manipulation in Python in Tamil 

1. Concatenation (+)

2. Repetition(*)

3. Membership Testing(in/not in)

4. Indexing -[]

5. Slicing - [start:stop:step]

1.Concatenation : (addition)

For example:

    l1=[‘Red’,’Green’]

    l2=[10,20,30]

    >>> l1=['red','blue']

    >>> l2=[10,20,30]

    >>> l3=l1+['yellow']

    >>> l3

    ['red', 'blue', 'yellow']

    >>> l1

    ['red', 'blue']

    >>> l3

    ['red', 'blue', 'yellow']

    >>> l4=l1+l2

    >>> l4

    ['red', 'blue', 10, 20, 30]

    >>> l5=['Green','white']+['Black']

    >>> l5

    ['Green', 'white', 'Black']

    >>> l1=l2+40

    Traceback (most recent call last):

    File "<pyshell#19>", line 1, in <module

    l1=l2+40

    TypeError: can only concatenate list (not "int") to list

    >>> l1=l2+'python'

    Traceback (most recent call last)

    File "<pyshell#20>", line 1, in <module>

    l1=l2+'python'

    TypeError: can only concatenate list (not "str") to list

    >>>

2.Repetition/Replication(*) 

    Multiply (* asterisk) operators replicates the list for a specifies number of times and creates a new list.

For example

    >>> list1=[10,20,30]    

    >>> list1*2

    [10, 20, 30, 10, 20, 30]

    >>> list1*3

    [10, 20, 30, 10, 20, 30, 10, 20, 30]

    >>> list1*4

    [10, 20, 30, 10, 20, 30, 10, 20, 30, 10, 20, 30]

    >>> x='python'*3

    >>> x

    'pythonpythonpython'

    >>>

3.Membership Operators:

       Membership testing is an operation carried out to check or test whether a particular elements /items is a member of that sequence or not.

For Example:

    >>> x=[10,20,30,40,50,100]

    >>> print(100 in x)

    True

    >>> print(100 not in x)

    False

    >>> print(12 not in x)

    True

    >>>

4.Indexing:

    Index is nothing but there is an index values for each item present in the sequence.

     >>> x=[10,20,30,40,50,100]

     >>> x[0]

           10

     >>> x[-1]

           100

     >>> x[4]

           50

     >>>

5.Slicing:

    >>> x=[1,2,3,4,5,6]

    >>> x[0:3]

           [1, 2, 3]

    >>> x[0:5]

           [1, 2, 3, 4, 5]

    >>> x[1:5]

           [2, 3, 4, 5]

    >>> x[1:5:-1]

           []

    >>> x[0:5:-1]

           []

    >>> x[1:3:2]

           [2]

    >>> x[0:5:2]

           [1, 3, 5]

    >>> x[0:5:-1]

           []

    >>>

Nested List:

For example:

   >>> x=[1,2,3,[10,20,30,40],'a','b']

   >>> x[4]

           'a'

   >>> x[3]

         [10, 20, 30, 40]

   >>> x[3][1]

         20

   >>>

Coping List:

            Method 1:

            L1=L2

   Method 2:

            L3=L1[:]

    Method 3:

            L4=list(L3)

    Method 4:

            L5=L4.copy()




Post a Comment

0 Comments