0% found this document useful (0 votes)
14 views

List Function

Uploaded by

itssohail099
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

List Function

Uploaded by

itssohail099
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

• Python provides many built-in functions and methods for list

manipulation.
• The syntax of using list functions is :
• ListObjectName.functionName()
• There are many list functions like:
• index()
• append()
• extend()
• insert()
• pop()
• remove()
• clear()
• count()
• reverse()
• sort()
• This function is used to get the index of first matched item from
the list. It returns index value of item to search.
• For example
>>> L1= [10,20,30,40,50,20]
>>> L1. index (20)
1 # item first matched at index 1
Note: if we pass any element which is not in the list then index
function will return an error: VALUe Error: n is not in the list
>>> L1. index (100) #Error
• This function is used to add items to the end of the list.
• For example
>>> family= [“father”,” mother”,” bro”,” sis”]
>>> family. append(“Tommy”)
>>> family
[“father”,”mother”,”bro”,”sis”,”Tommy”]
Note: append () function will add the new item but not return any value.
Let us understand this:
>>> L1= [10,20,30]
>>> L2=L1.append(40)
>>> L2 # Empty will not store any item of L1
>>> L1
[10, 20, 30, 40]
• This function is also used for adding multiple items. With extend we
can add only “list” to any list. Single value cannot be added using
extend ()
• For example
>>> subject1=["physics","chemistry","cs"]
>>> subject2=["english","maths"]
>>> subject1.extend(subject2)
>>> subject1
['physics', 'chemistry', 'cs', 'english', 'maths']
Note: here subject1 will add the contents of subject2 in it without
effecting subject2
Like append(), extend() also does not return any value.
>>> subject3 = subject1.extend(subject2)
>>>subject3 # Empty
• append () allows to add only 1 items to a list, extend () can add
multiple items to a list.

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


>>> m2= [100,200]
>>> m1. append (5)
>>> m1
[1, 2, 3, 4, 5]
>>> m1. append (6,7)

Traceback (most recent call last):


File "<pyshell#16>", line 1, in <module>
m1.append(6,7)
TypeError: append() takes exactly one argument (2 given)
>>> m1. append ([6,7])
>>> m1
[1, 2, 3, 4, 5, [6, 7]]
>>> len(m1)
6
Now let us see extend () function
>>> m2
[100, 200]
>>> m2. extend (300)

Traceback (most recent call last):


File "<pyshell#21>", line 1, in <module>
m2.extend(300)
TypeError: 'int' object is not iterable
>>> m2. extend ([300,400])
>>> m2
[100, 200, 300, 400]
>>> len(m2)
4
• This function is used to add elements to list like append () and extend
(). However, both append () and extend () insert the element at the
end of the list. But insert () allows us to add new elements anywhere in
the list i.e. at position of our choice.
ListObject.insert(Position, item)

>>> L1= [10,20,30,40,50]


>>> L1
[10, 20, 30, 40, 50]
>>> L1. insert (3,35)
>>> L1
[10, 20, 30, 35, 40, 50]
>>>
>>> L1= [10,20,30,40,50]
>>> L1
[10, 20, 30, 40, 50]
>>> L1. insert (0,5) #beginning
>>> L1
[5,10, 20, 30, 40, 50]
>>> L1. insert(len(L1),100) #last
>>> L1
[5,10, 20, 30, 40, 50,100]
>>> L1. insert (-10,2)
>>> L1
[2,5,10, 20, 30, 40, 50,100]
• We have read about this function earlier, it is used to remove item
from list.
ListObject.pop([index]) #if index is not passed last item will be deleted

>>> L1=[10,20,30,40,50]
>>> L1.pop()
50
>>> val = L1.pop(2)
>>> val
30
>>> L1
[10, 20, 40]
>>>
• The pop() method raises an exception(run time error) if the
list is already empty.
>>> L1= [ ]
>>> L1.pop()

Traceback (most recent call last):


File "<pyshell#19>", line 1, in <module>
L1.pop()
IndexError: pop from empty list
pop() function is used to remove element whose position is given, but
what if you know the value to be removed, but you dont know its index or
position in the list? Answer is: remove()
It remove the first occurance of given item from the list and return error if
there is no such item in the list. It will not return any value.
>>> L1.remove(5)
>>> L1
[1, 3, 7, 9, 11, 3, 7]
>>> L1.remove(3)
>>> L1
[1, 7, 9, 11, 3, 7]
>>> L1.remove(10) # Error
This function removes all teh items from the list and the list
becomes empty list.
List.clear()
>>> L1=[10,20,30,40,50]
>>> L1.clear()
>>> L1
[]
Note: UNlike ‘del listname’ statement, clear() will removes only the
elements and not the list. After clear() the list object still exists as an
empty list
This function returns the count of the item that you pased as an
argument. If the given item is not in the list, it returns zero.
>>> L1=[10,20,30,40,20,30,100]
>>> L1.count(20)
2
>>> L1.count(40)
1
>>> L1.count(11)
0
This function reverses the items in the list. This is done i place
i.e. It wil not create a new list . The syntax to use reverse() is:
>>> L1=[10,20,30,40,20,30,100]
>>> L1.reverse()
>>> L1
[100, 30, 20, 40, 30, 20, 10]
>>> L2=[11,22,33]
>>> L3=L2.reverse() #it will not return any value
>>> L3 # empty
[]
This function sorts the items of the list, by default increasing
order. This is done «in place» i.e. It does not create new list.
>>> L1=[10,1,7,20,8,9,2]
>>> L1.sort()
>>> L1
[1, 2, 7, 8, 9, 10, 20]
>>> L2=['g','e','a','c','b','d']
>>> L2.sort()
>>> L2
['a', 'b', 'c', 'd', 'e', 'g']
>>> L1.sort(reverse=True) # for descending order
>>>L1
[20, 10, 9, 8, 7, 2, 1]

You might also like