LIST OPERATIONS =============== LIST IS MUTABLE: ============== ld = eval(input("Enter a list data:")) print(id(ld)) print(ld[0]) ld[0] = 100 print(id(ld)) print(ld) ================================== count() ===== ==> use to find the total number of occurrences of the specified element within the list. Syntax: list_data/list_object.count(element) ==> If the specified element in count() is not the member of the list: return = '0' ==> we cannot count the number of occurrences of the specified within the range. ld = [1,2,3,4,5,1,3,5,7,9,1,2,3,4,5,1,3,5,7,9,2,4,6,8,10] # if the specified element is the member of the list print(ld.count(1)) print(ld.count(7)) # if the specified element is not the member of the list print(ld.count(20)) # counting the element within the range, canot count # print(ld.count(2,1,10)) =============================================== index() ===== ==> use to find the first occurrence of the specified element in the given list. Syntax: list_data/list_object.index(element) ==> When the specified element is not a member of the list: index() returns "Value Error". ==> index() can allow to find the element within the specified range. Syntax: list_data/list_object.index(element,start,stop) ld = [1,2,3,4,5,5,4,3,2,1,1,3,5,7,9] # when the specified element is the member of list # first occurrence of element is returned. print(ld.index(9)) print(ld.index(2)) # when the specified element is not the member of list # return: value error # print(ld.index(100)) # finding in between range print(ld.index(5,1,10)) ===================================== Adding of new elements into list ======================== 1) append() ========= ==> use to add any element into the list at the last/end by default. Syntax: list_data/list_object.append(element) ld = list() print("The List before the append operation is = ") print(ld) ld.append(10) ld.append(20) ld.append(30) ld.append(40) ld.append(50) print(ld) ======================================= 2) insert() ======== ==> Use to add a new element at the given index position Syntax: list_data/list_object.insert(index,element) ==> if the specified index is from out of range: the insertion of an element is at the last index automatically. ld = [1,3,5,7,9] print(ld) ld.append(20) ld.insert(1,100) # if the specified index is exceeded the index range ld.insert(10,1000) print(ld) =========================================== 3) extend() ======== ==> to extend a list of data to another list. ex: l1 l2 l1 <====== l2 Syntax: source_list_data.extend(destination_list_data)\ ld1 = [1,2,3,4] ld2 = [5,6,7,8] print("ld1 = ",ld1) print("ld2 = ",ld2) ld1.extend(ld2) print("ld1 = ",ld1) print("ld2 = ",ld2) ld2.extend(ld1) print("ld1 = ",ld1) print("ld2 = ",ld2) =============================================================== Note: ==== Method/Function ==> define the functionality functionality: take inputs process the inputs give an output sum() ==> 12, 24 12 + 24 36 is count() return anything? ==================== yes, an integer ==> count of occurrences of the specified element is index() return anything? ==================== yes, an integer ==> the first index value of the specified element is append() return anything? ====================== yes, None ld = [1,2,3,4,5,2,2] x = ld.count(2) # 1 print(x) y = ld.index(2) print(y) z = ld.append(100) print(z) ============================================= Removal of elements from list ======================= 1) remove() ========= ==> use to remove the specified element only from the given list. Syntax: list_data/list_object.remove(element) ==> if the specified element is unknown to the given list: remove() return: Value Error. ld = [1,2,3,4,5,6,7,8,9,10] print(ld) print(ld.remove(3)) # ld.remove(5,6) # if the specified element is not the member of a lis ==> Value Error # ld.remove(100) print(ld) ================================================ 2) pop() ====== ==> use to remove the last element from the given list automatically. Syntax: list_data.pop() ==> we can use pop() to remove the specified element based on the index. Syntax: list_data.pop(index) ld = [1,2,3,4,5] print(ld) # ld.pop() # ld.pop() ld.pop(1) print(ld) ========================================== del === ==> del is an attribute/property use to delete any data permanenlty. Syntax: del value_name ==> using del property, we can delete any element from the list based on the index ==> even we can also delete the entire list also permanently. a = 100 b = 200 l1 = [10,20,30,40,50] print(a,b) del a print(l1) del l1[2] # print(a,b) print(l1) del l1 # print(l1)