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

Pre-defined functions list-1

The document provides a comprehensive list of pre-defined functions for Python lists, detailing their syntax and usage. Functions include append(), insert(), clear(), remove(), pop(), index(), count(), copy(), reverse(), sort(), and extend(), each with examples demonstrating their functionality. It also highlights the behavior of these functions, such as handling errors and modifying list contents.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Pre-defined functions list-1

The document provides a comprehensive list of pre-defined functions for Python lists, detailing their syntax and usage. Functions include append(), insert(), clear(), remove(), pop(), index(), count(), copy(), reverse(), sort(), and extend(), each with examples demonstrating their functionality. It also highlights the behavior of these functions, such as handling errors and modifying list contents.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

==========================================

Pre-defined functions list


==========================================
=>We know that , on the object of list, we can perform both Indexing and Slicing
Operations.
=>Along with these operations, we can also perform many more operations on the
object of
list by using pre-defined function which are present in list object.

-------------------------------------
1) append()
-------------------------------------
=>This Function is used for adding the value to the list object always at end.
=>Syntax:- listobj.append(value)

Examples:
----------------------
>>> l1=[]
>>> print(l1,len(l1), id(l1))
[] 0 2722448959936
>>> l1.append(10)
>>> l1.append(10.5)
>>> print(l1,len(l1), id(l1))
[10, 10.5] 2 2722448959936
>>> l1.append("Rossum")
>>> print(l1,len(l1), id(l1))
[10, 10.5, 'Rossum'] 3 2722448959936
>>> l1.append(True)
>>> print(l1,len(l1), id(l1))
[10, 10.5, 'Rossum', True] 4 2722448959936
-----------------------------------------------------------------------------------
-----
2) insert()
-----------------------------------------------------------------------------------
-----
=>This function is used for inserting the values at valid existing index.
=>Syntax: listobject.insert(index,Value)
=>here Index can eithet +ve or -ve.
---------------------
Examples:
--------------------
>>> l2=[10,"Rossum",34.56,True]
>>> print(l2,id(l2))
[10, 'Rossum', 34.56, True] 2722448006784
>>> l2.insert(2,"Python")
>>> print(l2,id(l2))
[10, 'Rossum', 'Python', 34.56, True] 2722448006784
>>> l2[-2]=96.66
>>> print(l2,id(l2))
[10, 'Rossum', 'Python', 96.66, True] 2722448006784
-----------------------------------------------------------------------------------
------
3) clear():
----------------
=>This Function is used for removing all the elements of list.
=>Syntax:- listobj.clear()
------------------
Examples:
-----------------
>>> l1=[100,"Rossum",45.56,"Python",True]
>>> print(l1)
[100, 'Rossum', 45.56, 'Python', True]
>>> len(l1)
5
>>> l1.clear()
>>> print(l1)
[]
>>> len(l1)
0
-------------------------------------------------------
4) remove():----Based Value
------------------------------------------------------
=>This is used for removing First occurence of specified element from list.
=>If the specified element not found in list then we get ValueError.
=>Syntax:- listobj.remove(Value)
---------------------
Examples:
------------------
>>> l1=[100,"Rossum",45.56,"Python",True]
>>> print(l1)
[100, 'Rossum', 45.56, 'Python', True]
>>> l1.remove(100)
>>> print(l1)
['Rossum', 45.56, 'Python', True]
>>> l1.remove(45.56)
>>> print(l1)
['Rossum', 'Python', True]
>>> l1.remove(True)
>>> print(l1)
['Rossum', 'Python']
>>> l1.remove(100)--------------ValueError: list.remove(x): x not in list
>>> [].remove(0)-------------ValueError: list.remove(x): x not in list
>>> list().remove(10)----ValueError: list.remove(x): x not in list
-------------------------------------------------------------
>> l2=[10,20,10,30,10,20,20,10]
>>> print(l2)
[10, 20, 10, 30, 10, 20, 20, 10]
>>> l2.remove(10)
>>> print(l2)
[20, 10, 30, 10, 20, 20, 10]
>>> l2.remove(10)
>>> print(l2)
[20, 30, 10, 20, 20, 10]
-----------------------------------------------------------------------------------
------------------------------------
5) pop(index)
-----------------------------------------------------------------------------------
------------------------------------
=>This is used for removing the elements from list based on Index.
=>The Index can be either +ve or -ve
=>If the Index Valid Then the corresponding element removed from list and returned
=>If the Index InValid Then we get IndexError.
----------------
Examples:
----------------
>>> l2=[10,20,10,30,10,20,20,10]
>>> print(l2)
[10, 20, 10, 30, 10, 20, 20, 10]
>>> l2.pop(2)
10
>>> print(l2)
[10, 20, 30, 10, 20, 20, 10]
>>> l2.pop(-1)
10
>>> print(l2)
[10, 20, 30, 10, 20, 20]
>>> l2.pop(1)
20
>>> l2.pop(11)-----------------IndexError: pop index out of range
--------------------------------------------------------------------------
>>> l1=[100,"Rossum",45.56,"Python",True]
>>> l1.pop(1)
'Rossum'
>>> print(l1)
[100, 45.56, 'Python', True]
>>> l2.pop(22)-------------IndexError: pop index out of range
>>> [].pop(0)-------------IndexError: pop from empty list
>>> list().pop(-1)----IndexError: pop from empty list
-----------------------------------------------------------------------------------
-----------------------------
6) pop()
-----------------------------------------------------------------------------------
-----------------------------
=>This is used for removing last element of list.
=>When we call this function upon empty list object then we get IndexError.
==>Syntax: listobj.pop()
-----------------------
Examples:
----------------------
>>> l1=[100,"Rossum",45.56,"Python",True]
>>> print(l1)
[100, 'Rossum', 45.56, 'Python', True]
>>> l1.pop()
True
>>> print(l1)
[100, 'Rossum', 45.56, 'Python']
>>> l1.insert(2,"Python")
>>> print(l1)
[100, 'Rossum', 'Python', 45.56, 'Python']
>>> l1.pop()
'Python'
>>> print(l1)
[100, 'Rossum', 'Python', 45.56]
>>> l1.pop()
45.56
>>> print(l1)
[100, 'Rossum', 'Python']
>>> l1.pop()
'Python'
>>> print(l1)
[100, 'Rossum']
>>> l1.pop()
'Rossum'
>>> print(l1)
[100]
>>> l1.pop()
100
>>> print(l1)
[]
>>> l1.pop()------------IndexError: pop from empty list
-----------------------------------------------------------------------------------
---------------------------------------
NOTE: del operator
-----------
=>The operator "del" is used for removing
a) Based on Indexing ----- del obj[Index]
b) Based on Slicing------- del obj[Begin:End:Step
c) Complete object------- del obj

Examples:
-------------------
>>> l1=[100,"Rossum",45.56,"Python",True]
>>> print(l1)----------------[100, 'Rossum', 45.56, 'Python', True]
>>> del l1[2] # Index Based Removal
>>> print(l1)----------------[100, 'Rossum', 'Python', True]
>>> del l1[1:3] # Slicing Based Removal
>>> print(l1)----------------------[100, True]
>>> del l1 # Removing entire object
>>> print(l1)-------NameError: name 'l1' is not defined.
-----------------------------------------------------------------------------------
---------------
7) index()
------------------
=>This function obtains Index of First Occurence of the Specified Element.
=>Syntax:- lisobj.index(Value)
=>If the value does not exists then we get ValueError.

Examples:
-------------------
>>> l2=[10,20,10,30,10,20,20,10]
>>> print(l2)
[10, 20, 10, 30, 10, 20, 20, 10]
>>> l2.index(10)
0
>>> l2.index(100)---------------ValueError: 100 is not in list
>>> l2.index(20)
1
-----------------------------------------------------------------------------------
---------------------
8) count():
--------------------------------------
=>This function find number of occurences of a perticular element.
=>Syntax: listobj.count(value)
=>=>If the value does not exists then this function gives 0.
--------------------------------------------------------
Examples:
---------------------
>>> l2=[10,20,10,30,10,20,20,10]
>>> print(l2)
[10, 20, 10, 30, 10, 20, 20, 10]
>>> l2.count(10)
4
>>> l2.count(20)
3
>>> l2.count(30)
1
>>> l2.count(300)
0
>>> list().count(0)
0
--------------------------------------------------------
>>> list([10,10,0]).count(0)
1
>>> list([10,10,0]).count(10)
2
>>> list([10,10,0]).count(100)
0
------------------------------------------------------------------------------
9) copy():
-----------------------------------------------------------------------------
=>This function is used for copying the content of one object into another object
( implements Shallow Copy)
=>Syntax: listobj2=listobj1.copy()
------------------
Examples:
-------------------
>>> l1=[10,"Rossum"]
>>> print(l1,id(l1))
[10, 'Rossum'] 1843503104896
>>> l2=l1.copy() # shallow Copy
>>> print(l2,id(l2))
[10, 'Rossum'] 1843499592064
>>> l1.append("Python")
>>> l2.insert(1,"Django")
>>> print(l1,id(l1))
[10, 'Rossum', 'Python'] 1843503104896
>>> print(l2,id(l2))
[10, 'Django', 'Rossum'] 1843499592064
-----------------------------------------------------------------
>>> l1=[100,"Rossum",45.56,"Python",True]
>>> l2=l1[1:4] # Slice Based Copy is called Shallow Copy
>>> print(l1,id(l1))
[100, 'Rossum', 45.56, 'Python', True] 1843499592064
>>> print(l2,id(l2))
['Rossum', 45.56, 'Python'] 1843503104896
--------------------------------------------------------------------------------
>>> l1=[100,"Rossum",45.56,"Python",True]
>>> l2=l1[::] # Slice Based Copy is called Shallow Copy
>>> print(l1,id(l1))
[100, 'Rossum', 45.56, 'Python', True] 1843498922432
>>> print(l2,id(l2))
[100, 'Rossum', 45.56, 'Python', True] 1843499592064
-----------------------------------------------------------------------------------
-----------------------------
10) reverse():
------------------------------
=>This is used for obtaining reverse of the elememnts of list ( front to back and
back to front).
=>Syntax:- listobj.reverse()
------------------------
Examples:
------------------------
>>> l1=[100,"Rossum",45.56,"Python",True]
>>> print(l1,id(l1))-----------[100, 'Rossum', 45.56, 'Python', True] 1843503104896
>>> l1.reverse()
>>> print(l1,id(l1))-----[True, 'Python', 45.56, 'Rossum', 100] 1843503104896
-----------------------------------------------------------------------------------
----------------------------
11) sort()
-----------------------------------------------------------------------------------
----------------------------
=>This function is used for sorting the data in Either Ascending order or Decending
Order
=>By default we get in Ascending Order
=>Syntax: listobject.sort(reverse=True or False)
=>Here When reverse=True the we get the lements of list in Decensing Order
=>Here When reverse=False ( by default) the we get the lements of list in Ascending
Order

Examples:
--------------------
>>> l1=[10,3,2,-4,14,12,56,0,34]
>>> print(l1)-----------------------[10, 3, 2, -4, 14, 12, 56, 0, 34]
>>> l1.sort() # default value of reverse=False
>>> print(l1)--------------------[-4, 0, 2, 3, 10, 12, 14, 34, 56]
--------------------------------------------------------
>>> l1=[10,3,2,-4,14,12,56,0,34]
>>> print(l1)---------------[10, 3, 2, -4, 14, 12, 56, 0, 34]
>>> l1.sort(reverse=False)
>>> print(l1)-------------------------[-4, 0, 2, 3, 10, 12, 14, 34, 56]
----------------------------------------------------------------
>>> l1=[10,3,2,-4,14,12,56,0,34]
>>> print(l1)-----------------[10, 3, 2, -4, 14, 12, 56, 0, 34]
>>> l1.sort(reverse=True)
>>> print(l1)------------[56, 34, 14, 12, 10, 3, 2, 0, -4]
--------------------
Examples:
-------------------
>>> l1=["Trump","Biden","Putin","Modi","Rossum","Adam"]
>>> print(l1)---------------------['Trump', 'Biden', 'Putin', 'Modi', 'Rossum',
'Adam']
>>> l1.sort(reverse=False)
>>> print(l1)-----------------['Adam', 'Biden', 'Modi', 'Putin', 'Rossum', 'Trump']
>>> l1=["Trump","Biden","Putin","Modi","Rossum","Adam"]
>>> print(l1)---------------['Trump', 'Biden', 'Putin', 'Modi', 'Rossum', 'Adam']
>>> l1.sort(reverse=True)
>>> print(l1)-------------['Trump', 'Rossum', 'Putin', 'Modi', 'Biden', 'Adam']
------------------
Examples:
------------------
>>> l1=[100,"Rossum",45.56,"Python",True]
>>> print(l1)
[100, 'Rossum', 45.56, 'Python', True]
>>> l1.sort()------------TypeError: '<' not supported between instances of 'str'
and 'int'
-----------------------------------------------------------------------------------
----------------------------
12) extend()
----------------------
=>This function is used for adding the content of one list object to another list
object.
=>Syntax: listobj1.extend(listobj2)
=>Here listobj2 are added to listobj1
Examples:
------------------
>>> l1=[10,20,30]
>>> l2=["RS","DR","TS"]
>>> print(l1)
[10, 20, 30]
>>> print(l2)
['RS', 'DR', 'TS']
>>> l1.extend(l2)
>>> print(l1)
[10, 20, 30, 'RS', 'DR', 'TS']
>>> print(l2)
['RS', 'DR', 'TS']
-----------------------------------------------------------------------------------
-------------------

You might also like