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

    List Manipulation In Python

LIST Methods / Functions

 YouTube Video: List Manipulation in Python in Tamil 

Build-in functions :

1. append()

2. extend()

3. index()

4. insert()

5. sort()

6. sorted()

7. pop()

8. remove()

9. reverse()

10.max(),min(),sum()

11.clear()

12.len()

13.count()

14.del ()

 

 1.append()

Add item to the end of the list.

Syntax:

    list.append(item)

For example:

    >>> l1=[1,2,3,4,5]

    >>> l1.append(100)

    >>> l1

        [1, 2, 3, 4, 5, 100]

    >>> l1=[1,2,3,40,67,'a',"10+j23","python",20.34,100]

    >>> l1.append(99)

    >>> l1

        [1, 2, 3, 40, 67, 'a', '10+j23', 'python', 20.34, 100, 99]

    >>> l1.append("list")

    >>> l1

        [1, 2, 3, 40, 67, 'a', '10+j23', 'python', 20.34, 100, 99, 'list']

    >>> l1.append(10,20)

        Traceback (most recent call last):

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

        l1.append(10,20)

        TypeError: append() takes exactly one argument (2 given)

    >>>

2. Extend()

    Add one list at the end of the another list.In other words,all the items of a list are added at the end of an already created list

Syntax()

    list1.extend(list2)

For example:

    >>> l1=[1,2,3,4]

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

    >>> l1.extend(l2)

    >>> l1

        [1, 2, 3, 4, 10, 20, 30]

    >>> l2

        [10, 20, 30]

    >>>

3.index()    

    Return the index of the first element whose values is equal to item.A valuesError exception is raised if item is not found in the list.

Syntax:

list.index(item)

For example

>> l1

    [1, 2, 3, 4, 5, 100]

>>> l1.index(5)

    4

>>> l1.index(50)

    Traceback (most recent call last):

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

    l1.index(50)

    ValueError: 50 is not in list

>>>

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

>>> l1=[1,2,3,4,5,6,10,40,50]

>>> l1[40]

    Traceback (most recent call last):

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

    l1[40]

     IndexError: list index out of range

>>> l1[7]

    40

>>> l1.index(10)

    6

>>>

4.insert()

    Inserts item into the list at the specified index. When an item is inserted into a list, the list is expanded in size to accommodate the new item. The item that was previously at the specified index and all the items after it are shifted by one position toward the end of the list.No exceptions will occur if you specify an invalid index.If you specify an index beyond the end of the list , the item will be added to the end of the listIf you use negative index that specifies an invalid position, the item will be inserted at the beginning of the list.

Syntax:

    insert(index,item)

For example:

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

    >>> l1.insert(1,2200)

    >>> l1

        [10, 2200, 20, 30]

    >>> l1.insert(4,100)

    >>> l1

        [10, 2200, 20, 30, 100]

   >>> l1.insert(-1,500)

   >>> l1

        [10, 2200, 20, 30, 500, 100]

    >>> l1.insert(-3,250)

    >>> l1

       [10, 2200, 20, 250, 30, 500, 100]

    >>> l1.insert(8,800)

    >>> l1

      [10, 2200, 20, 250, 30, 500, 100, 800]

    >>>

example 2:

    >>> #insert

    >>> #insert(index,item)

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

    >>> l1.insert(1,100)

    >>> l1

       [1, 100, 2, 3, 4, 5, 6]

    >>> l1.insert(0,90)

    >>> l1

      [90, 1, 100, 2, 3, 4, 5, 6]

    >>> l1.insert(-1,200)

    >>> l1

     [90, 1, 100, 2, 3, 4, 5, 200, 6]

    >>> l1.insert(len(l1),1000)

    >>> l1

      [90, 1, 100, 2, 3, 4, 5, 200, 6, 1000]

    >>> 

5.sort()

Sorts the item in the list so they appear in ascending order (From the lowest values highest values)

Syntax:

    list.sort()

For example

    >>> l1.sort()

    >>> l1

        [10, 20, 30, 100, 250, 500, 800, 2200]

    >>>

    >>> l1.sort()

    >>> l1

        [10, 20, 30, 100, 250, 500, 800, 2200]

    >>> l1.sort(reverse=True)

    >>> l1

        [2200, 800, 500, 250, 100, 30, 20, 10]

    >>> l1.sort(reverse=False)

    >>> l1

        [10, 20, 30, 100, 250, 500, 800, 2200]

    >>> l1.sort(reverse=True)

    >>> l1

        [2200, 800, 500, 250, 100, 30, 20, 10]

    >>> l1.sort()

    >>> l1

        [10, 20, 30, 100, 250, 500, 800, 2200]

    >>>

6. sorted()

Syntax:

sorted(list)

For Example:

>>> l1=[10,23,4,6,2,7,45,67]

>>> l1.sorted()

  Traceback (most recent call last):

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

   l1.sorted()

   AttributeError: 'list' object has no attribute 'sorted'

>>> l1.sorted(reverse=True)

   Traceback (most recent call last):

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

   l1.sorted(reverse=True)

   AttributeError: 'list' object has no attribute 'sorted'

>>> sorted(l1)

   [2, 4, 6, 7, 10, 23, 45, 67]

7. Clear()

    Remove the all the items in a list

Syntax:

    list.clear()

Example:

>>> l1

    [1, 2, 3, 4, 5]

>>> l1.clear()

>>> l1

    []

>>>

8. count()

    How many times an element has occurred in a list and returns it. If the element is not present its generate the result is zero

Syntax:

    list.count(element)

For Example:

>>> l1=[10,2,1,2,2,1,1,1,1,4,3,2,56,2,2,2]

>>> l1.count(2)

    7

>>> l1.count(1)

    5

9. len()

    Returns the length of the list

Syntax;

    len(list)

Example:

>>> l1

    [1, 2, 3, 4, 5]

>>> len(l1)

    5

>>>

10. reverse()

    Reverse the order of the items in the list

Syntax:

    list.reverse()

For example:

>>> l1=[1,45,23,89,70,2]

>>> l1.reverse()

>>> l1

    [2, 70, 89, 23, 45, 1]

>>>

11.Updating list

Syntax:

    L1[index]=<new_value>

For Example:

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

>>> l2[2]=23

>>> l1

    [10, 2, 1, 2, 2, 1, 1, 1, 1, 4, 3, 2, 56, 2, 2, 2]

>>> l2

    [10, 20, 23]

>>> l2[0]=100

>>> l2

    [100, 20, 23]

>>>

 DELETION OPERATION

11. pop()

    Remove the end of the item in a list

Syntax()

   list.pop()

For Example:

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

>>> l1.pop()

    6

>>> l1

    [1, 2, 3, 4, 4, 5, 5, 5, 6, 6]

>>> l1.pop()

    6

>>> l1

    [1, 2, 3, 4, 4, 5, 5, 5, 6]

>>>

8.remove()

    Removes the first occurrence of item from the list. A valueError exception is raised if item is not found in the list.

Syntax:

    list.remove(item)

For example:

>>> l1=[10,20,30,40,50,60,40]

>>> l1.remove(40)

>>> l1

    [10, 20, 30, 50, 60,40]

>>>

12. Del statements

Syntax:

    del list[index]

For Example:

>>> l1

    [1, 2, 3, 4, 4, 5, 5, 5, 6]

>>> del l1[4]

>>> l1

    [1, 2, 3, 4, 5, 5, 5, 6]

>>> del l1[6]

>>> l1

    [1, 2, 3, 4, 5, 5, 6]

>>> del l1[8]

    Traceback (most recent call last):

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

    del l1[8]

    IndexError: list assignment index out of range

>>>

13.max()

Syntax()

>>> l1

    [2, 70, 89, 23, 45, 1]

>>> max(l1)

    89

>>> min(l1)

    1

>>>

    Mean=sum of elements/total number of elements.

    l1=[2, 2, 2, 1, 1, 1, 1, 4, 3, 2, 56, 2, 2, 2]

>>> sum(l1)

    81

>>> 

Post a Comment

0 Comments