===============================================================
Pre-Defined Functions in list--Most Imp
===============================================================
=>We Know that on the Object of List, we can Perform the following Operations
=>We can Get Single Element by Using Indexing
=>We can get Multiple Elements By Using Slicing
=>We can Update Single Element by Using Indexing
=>We can Update Multiple Elements by Using Slicing
=>Along with the above Operations, we can Perform the Following Operations by using
Pre-Defined Functions Present in list object.
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
1. append()
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
=>Syntax: listObj.append(Value)
=>This function is used for adding the Value at the end of existing elements of
list (Known as appending)
-------------------------
Examples
------------------------
>>> lst=[10,"RS",34.56]
>>> print(lst,type(lst),id(lst))
[10, 'RS', 34.56] <class 'list'> 1362827952000
>>> lst.append("NL")
>>> print(lst,type(lst),id(lst))
[10, 'RS', 34.56, 'NL'] <class 'list'> 1362827952000
>>> lst.append(True)
>>> print(lst,type(lst),id(lst))
[10, 'RS', 34.56, 'NL', True] <class 'list'> 1362827952000
-----------------------------
>>> lst1=[]
>>> print(lst1,type(lst1),id(lst1))
[] <class 'list'> 1362827948672
>>> lst1.append(100)
>>> lst1.append("DR")
>>> lst1.append(True)
>>> print(lst1,type(lst1),id(lst1))
[100, 'DR', True] <class 'list'> 1362827948672
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
2. insert()
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
=>Syntax: ListObj.insert(index,Value)
=>This Function is used for adding the value to the list at Specified Index.
=>Here the Index can either +ve or -ve
-----------------------------------------
Examples
-----------------------------------------
>>> lst=[10,"RS",45.67,"PYTHON"]
>>> print(lst,type(lst),id(lst))
[10, 'RS', 45.67, 'PYTHON'] <class 'list'> 2072237158528
>>> lst.insert(2,"NL")
>>> print(lst,type(lst),id(lst))
[10, 'RS', 'NL', 45.67, 'PYTHON'] <class 'list'> 2072237158528
>>> lst.insert(0,100)
>>> print(lst,type(lst),id(lst))
[100, 10, 'RS', 'NL', 45.67, 'PYTHON'] <class 'list'> 2072237158528
>>> lst.insert(-1,"HYD")
>>> print(lst,type(lst),id(lst))
[100, 10, 'RS', 'NL', 45.67, 'HYD', 'PYTHON'] <class 'list'> 2072237158528
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
3. remove()----------Based on Value
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
=>Syntax: ListObj.remove(Value)
=>This Function is used for Removing the First Occurence of Value from List
Object.
=>If the Specified Value does not Exist then we get ValueError.
-----------------------------
Examples
-----------------------------
>>> lst=[10,"RS",45.67,"PYTHON",2+3j]
>>> print(lst,type(lst),id(lst))
[10, 'RS', 45.67, 'PYTHON', (2+3j)] <class 'list'> 2072237387520
>>> lst.remove(45.67)
>>> print(lst,type(lst),id(lst))
[10, 'RS', 'PYTHON', (2+3j)] <class 'list'> 2072237387520
>>> lst.remove("PYTHON")
>>> print(lst,type(lst),id(lst))
[10, 'RS', (2+3j)] <class 'list'> 2072237387520
>>> lst.remove("JAVA")-------------------------ValueError: list.remove(x): x not in
list
-----------------------------------
>>> lst=[10,20,30,10,20,40,50,10,20]
>>> print(lst,type(lst),id(lst))
[10, 20, 30, 10, 20, 40, 50, 10, 20] <class 'list'> 2072237384576
>>> lst.remove(10)
>>> print(lst,type(lst),id(lst))
[20, 30, 10, 20, 40, 50, 10, 20] <class 'list'> 2072237384576
>>> lst.remove(10)
>>> print(lst,type(lst),id(lst))
[20, 30, 20, 40, 50, 10, 20] <class 'list'> 2072237384576
>>> lst.remove(10)
>>> print(lst,type(lst),id(lst))
[20, 30, 20, 40, 50, 20] <class 'list'> 2072237384576
>>> lst.remove(10)-----------------ValueError: list.remove(x): x not in list
------------------
>>> [].remove(10)----------------------ValueError: list.remove(x): x not in list
>>> list().remove(10)-----------------ValueError: list.remove(x): x not in list
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
4. pop(index)------------------Based on Index
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
=>Syntax: ListObj.pop(index)
=>This Function is used for Removing the Value from List Object Based on Index.
=>If we enter Invalid Index then we get IndexError.
----------------------------
Examples
----------------------------
>>> lst=[10,20,30,10,20,40,50,10,20]
>>> print(lst,type(lst),id(lst))
[10, 20, 30, 10, 20, 40, 50, 10, 20] <class 'list'> 2072237378176
>>> lst.pop(3)
10
>>> print(lst,type(lst),id(lst))
[10, 20, 30, 20, 40, 50, 10, 20] <class 'list'> 2072237378176
>>> lst.pop(-1)
20
>>> print(lst,type(lst),id(lst))
[10, 20, 30, 20, 40, 50, 10] <class 'list'> 2072237378176
>>> lst.pop(3)
20
>>> print(lst,type(lst),id(lst))
[10, 20, 30, 40, 50, 10] <class 'list'> 2072237378176
>>> lst.pop(30)------------------------------IndexError: pop index out of range
--------------------------
>>> [].pop(0)--------------------IndexError: pop from empty list
>>> list().pop(10)--------------IndexError: pop from empty list
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
5. pop()
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
=>Syntax: ListObj.pop()
=>This Function always removes the Last Value of List Object.
=>When we can this Function on Empty List Object then we get IndexError.
--------------------------------
Examples
--------------------------------
>>> lst=[10,20,30,10,20,40]
>>> print(lst,type(lst),id(lst))
[10, 20, 30, 10, 20, 40] <class 'list'> 2072229221568
>>> lst.pop()
40
>>> print(lst,type(lst),id(lst))
[10, 20, 30, 10, 20] <class 'list'> 2072229221568
>>> lst.pop()
20
>>> print(lst,type(lst),id(lst))
[10, 20, 30, 10] <class 'list'> 2072229221568
>>> lst.pop()
10
>>> print(lst,type(lst),id(lst))
[10, 20, 30] <class 'list'> 2072229221568
>>> lst.pop()
30
------------------------------------
>>> [].pop()--------------------IndexError: pop from empty list
>>> list().pop()---------------IndexError: pop from empty list
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
6. clear()
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
Syntax: listobj.clear()
=>This Function removes all the Values from List object.
=>When we call this function empty list object then we get
-------------------------------------
Examples
-------------------------------------
>>> lst=[10,20,30,10,20,40]
>>> print(lst,type(lst),id(lst))
[10, 20, 30, 10, 20, 40] <class 'list'> 2072237343488
>>> len(lst)
6
>>> lst.clear()
>>> print(lst,type(lst),id(lst))
[] <class 'list'> 2072237343488
>>> len(lst)
0
----------------------------------------
>>> [].clear() --------------- NO OUPUT
OR
>>> print([].clear())----------None
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
NOTE: del operator
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
Syntax-1: del MutableObject[Index]-------->Removes Single Value from Mutable
Object based on Index
Syntax-2: del MutableObject[StartIndex:EndIndex:Step]----->Removes Multiple
Values from Mutable Object Based on
Slicing
Syntax-3: del ImmutableObjectMutableObject---->Removes Complete Object Content
and Its Physical Memory space
-----------------------------
Examples
-----------------------------
>>> lst=[10,20,30,10,20,40]
>>> print(lst,id(lst))
[10, 20, 30, 10, 20, 40] 2072237330624
>>> del lst[0:3]
>>> print(lst,id(lst))
[10, 20, 40] 2072237330624
>>> del lst[0]
>>> print(lst,id(lst))
[20, 40] 2072237330624
>>> del lst
>>> print(lst,id(lst))-----------------NameError: name 'lst' is not defined
---------------------------------
>>> s="PYTHON"
>>> print(s,type(s))-----------------PYTHON <class 'str'>
>>> del s[0]---------------------------TypeError: 'str' object doesn't support item
deletion
>>> del s[0:3]------------------------TypeError: 'str' object does not support item
deletion
>>> del s
>>> print(s,type(s))------------------NameError: name 's' is not defined
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
7. index()
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
=>Syntax: ListObj.index(Value)
=>This Function is used for Finding Index of First Occurence of Specified Value.
=>The Specified Value does not exist then we ValueError.
------------------------
Examples
-------------------------
>>> lst=[100,200,300,100,400,500,200,100]
>>> print(lst,type(lst))
[100, 200, 300, 100, 400, 500, 200, 100] <class 'list'>
>>> lst.index(300)
2
>>> lst.index(100)
0
>>> lst.index(1000)--------------------ValueError: 1000 is not in list
----------------------
>>> list().index(10)----------------ValueError: 10 is not in list
>>> [].index(0)----------------------ValueError: 0 is not in list
>>> list("MISSISSIPPI").index("I")-------------1
>>> list("123412")---------------------------['1', '2', '3', '4', '1', '2']
>>> list("123412").index(1)--------------ValueError: 1 is not in list
>>> list("123412").index('1')---------------0
*******************************
enumerate()---------Most Imp
*******************************
=>This Function is used for Obtaining Index and Corresponing from any Iterable
Object (Contains More than one value).
Syntax:
-----------
for Index,Value in enumerate(Iterable-Object):
print(index,"--->",value)
-------------------------------------
Examples
-------------------------------------
>>> lst=[100,200,300,100,400,500,200,100]
>>> print(lst)------------[100, 200, 300, 100, 400, 500, 200, 100]
>>> for x in enumerate(lst):
... print(x)
...
(0, 100)
(1, 200)
(2, 300)
(3, 100)
(4, 400)
(5, 500)
(6, 200)
(7, 100)
>>> for index,value in enumerate(lst):
... print(index,"-->",value)
...
0 --> 100
1 --> 200
2 --> 300
3 --> 100
4 --> 400
5 --> 500
6 --> 200
7 --> 100
>>> for index,value in enumerate(lst):
... if(value==100):
... print(index,"-->",value)
...
0 --> 100
3 --> 100
7 --> 100
>>> for index,value in enumerate(lst):
... if(value==200):
... print(index,"-->",value)
...
1 --> 200
6 --> 200
----------------------
>>>s="PYTHON"
>>> for ind,val in enumerate(s):
... print(ind,"-->",val)
...
0 --> M
1 --> I
2 --> S
3 --> S
4 --> I
5 --> S
6 --> S
7 --> I
8 --> P
9 --> P
10 --> I
>>> for ind,val in enumerate(s):
... if(val=="I"):
... print(ind,"-->",val)
...
1 --> I
4 --> I
7 --> I
10 --> I
>>> for ind,val in enumerate(s):
... if(val=="S"):
... print(ind,"-->",val)
...
2 --> S
3 --> S
5 --> S
6 --> S
-----------------
>>> for ind,val in enumerate(range(100,111,2)):
... print(ind,"-->",val)
...
0 --> 100
1 --> 102
2 --> 104
3 --> 106
4 --> 108
5 --> 110
-----------------
>>> a=10
>>> for ind,val in enumerate(a): # TypeError: 'int' object is not iterable
... print(ind,"-->",val)
*******************************
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
8. copy()
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
=>Syntax: ListObj2=ListObj1.copy()
=>This Function is used for Copying the content of One List Object1 into another
List Object2
-------------------------------------
Examples--Shallow Copy
-------------------------------------
>>> lst1=[10,"RS",34.56]
>>> print(lst1,id(lst1))-----------------[10, 'RS', 34.56] 2664197782848
>>> lst2=lst1.copy() # Shallow Copy
>>> print(lst2,id(lst2))--------------[10, 'RS', 34.56] 2664197780736
>>> lst1.append("NL")
>>> lst2.insert(1,"GUIDO")
>>> print(lst1,id(lst1))-------------[10, 'RS', 34.56, 'NL'] 2664197782848
>>> print(lst2,id(lst2))-------------[10, 'GUIDO', 'RS', 34.56] 2664197780736
----------------------------------
Examples--Deep Copy
----------------------------------
>>> lst1=[10,"RS",34.56]
>>> print(lst1,id(lst1))-----------------[10, 'RS', 34.56] 2664197897600
>>> lst2=lst1 # Deep Copy
>>> print(lst2,id(lst2))---------------[10, 'RS', 34.56] 2664197897600
>>> lst1.append("NL")
>>> print(lst1,id(lst1))---------------[10, 'RS', 34.56, 'NL'] 2664197897600
>>> print(lst2,id(lst2))---------------[10, 'RS', 34.56, 'NL'] 2664197897600
>>> lst2.insert(1,"GD")
>>> print(lst1,id(lst1))--------------[10, 'GD', 'RS', 34.56, 'NL'] 2664197897600
>>> print(lst2,id(lst2))--------------[10, 'GD', 'RS', 34.56, 'NL'] 2664197897600
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
9. reverse()
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
=>Syntax: listobj2=lstobj1.reverse()
OR
listobj1.reverse()
=>This Function is used for Reversing the Elements of Listobj1 and reversed
Elements placed in listobj1 itself and never placed in listobj2
---------------------------------
Examples
---------------------------------
>>> lst1=[10,"RS",34.56]
>>> print(lst1,id(lst1))--------------[10, 'RS', 34.56] 2664239083520
>>> lst2=lst1.reverse()
>>> print(lst2)------------------------None
>>> print(lst1,id(lst1))--------------[34.56, 'RS', 10] 2664239083520
-------------------------------------
>>> lst1=[10,20,15,2,3,17]
>>> print(lst1,id(lst1))---------------[10, 20, 15, 2, 3, 17] 2664239086720
>>> lst1.reverse()
>>> print(lst1,id(lst1))---------------[17, 3, 2, 15, 20, 10] 2664239086720
---------------------------------
NOTE:
>>> lst1=[10,"RS",34.56]
>>> print(lst1,id(lst1))---------------[10, 'RS', 34.56] 2664239082240
>>> lst2=lst1[::-1]
>>> print(lst1,id(lst1))--------------[10, 'RS', 34.56] 2664239082240
>>> print(lst2,id(lst2))--------------[34.56, 'RS', 10] 2664239082304
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
10. sort()
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
=>Syntax1: ListObj.sort()-------------------------------->Sorts in Data in
Ascending order
=>Syntax2: ListObj.sort(reverse=False)------------>Sorts in Data in Ascending
order
=>Syntax3: ListObj.sort(reverse=True)------------->Sorts in Data in Decending
order
----------------------------------------------------
Examples
----------------------------------------------------
>>> lst=[10,2,4,15,6,-1,0,16]
>>> print(lst,id(lst))
[10, 2, 4, 15, 6, -1, 0, 16] 2664239078400
>>> lst.sort()
>>> print(lst,id(lst))
[-1, 0, 2, 4, 6, 10, 15, 16] 2664239078400
>>> lst.reverse()
>>> print(lst,id(lst))
[16, 15, 10, 6, 4, 2, 0, -1] 2664239078400
--------------------------------
>>> lst=[10,2,4,15,6,-1,0,16]
>>> print(lst,id(lst))----------[10, 2, 4, 15, 6, -1, 0, 16] 2664239079168
>>> lst.sort(reverse=False)
>>> print(lst,id(lst))---------------[-1, 0, 2, 4, 6, 10, 15, 16] 2664239079168
------------------------------
>>> lst=[10,2,4,15,6,-1,0,16]
>>> print(lst,id(lst))----------[10, 2, 4, 15, 6, -1, 0, 16] 2664239076992
>>> lst.sort(reverse=True)
>>> print(lst,id(lst))------------[16, 15, 10, 6, 4, 2, 0, -1] 2664239076992
----------------------------------------
>>> lst=["Rossum","Trump","Travis","Dennis","Ritche"]
>>> print(lst)
['Rossum', 'Trump', 'Travis', 'Dennis', 'Ritche']
>>> lst.sort()
>>> print(lst)
['Dennis', 'Ritche', 'Rossum', 'Travis', 'Trump']
>>>
>>> lst=["Rossum","Trump","Travis","Dennis","Ritche"]
>>> print(lst)
['Rossum', 'Trump', 'Travis', 'Dennis', 'Ritche']
>>> lst.sort(reverse=True)
>>> print(lst)
['Trump', 'Travis', 'Rossum', 'Ritche', 'Dennis']
----------------------------------------
>>> lst=[10,"RS",34.56,2+3j]
>>> print(lst)---------------[10, 'RS', 34.56, (2+3j)]
>>> lst.sort()---------------TypeError: '<' not supported between instances of
'str' and 'int'
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
11. extend()
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
Syntax: Listobj1.extend(Listobj2)
=>This Function is used for merging the content of listobj2 with listobj1.
NOTE: Listobj1.extend(Listobj2, ListObj3,.....,ListObj-n)--------Gives Type
Error
Alternatively
Listobj1.extend(Listobj2)
Listobj1.extend(Listobj3)
---------------------------------
Listobj1.extend(Listobj-n)
OR
Syntax: Listobj1=Listobj1+Listobj2+ ListObj3+.....+ListObj-n
-------------------------------------
Examples
-------------------------------------
>>> lst1=[10,20,30]
>>> lst2=["RS","TR"]
>>> print(lst1)
[10, 20, 30]
>>> print(lst2)
['RS', 'TR']
>>> lst1.extend(lst2)
>>> print(lst1)
[10, 20, 30, 'RS', 'TR']
>>> print(lst2)
['RS', 'TR']
-----------------
>>> lst1=[10,20,30]
>>> lst2=["RS","TR"]
>>> lst2.extend(lst1)
>>> print(lst2)
['RS', 'TR', 10, 20, 30]
-------------------------------
>>> lst1=[10,20,30]
>>> lst2=["RS","TR"]
>>> lst3=[1.2,3.4,5.6]
>>> lst1.extend(lst2,lst3)----------------TypeError: list.extend() takes exactly
one argument (2 given)
To avoid above error--we use the following solution
>>> lst1.extend(lst2)
>>> lst1.extend(lst3)
>>> print(lst1)---------------------------[10, 20, 30, 'RS', 'TR', 1.2, 3.4, 5.6]
Alternative solution is here by using + Operator
>>> lst1=[10,20,30]
>>> lst2=["RS","TR"]
>>> lst3=[1.2,3.4,5.6]
>>>
>>> print(lst1,id(lst1))
[10, 20, 30] 2664238817984
>>>
>>> lst1=lst1+lst2+lst3
>>> print(lst1,id(lst1))---------------[10, 20, 30, 'RS', 'TR', 1.2, 3.4, 5.6]
2664238823168
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
12. count()
-----------------------------------------------------------------------------------
---------------------------------------------------------
=>Syntax: ListObj.count(Value)
=>This Function is used for Finding the Number of Occurences (Frequency) of
Specified Value.
=>If the Specified Value does not Exist then we get 0 as Count
-----------------------------
Examples
-----------------------------
>>> lst=[10,20,30,10,20,30,40,50,10]
>>> print(lst)
[10, 20, 30, 10, 20, 30, 40, 50, 10]
>>> lst.count(10)
3
>>> lst.count(20)
2
>>> lst.count(30)
2
>>> lst.count(300)
0
>>> s="MISSISSIPPI"
>>> lst=list(s)
>>> print(lst)
['M', 'I', 'S', 'S', 'I', 'S', 'S', 'I', 'P', 'P', 'I']
>>> lst.count("I")
4
>>> lst.count("S")
4
-----------------------------------------------------------------------------------
--------------------------------------------------------