LIST OPERATIONS =============== Counting the number of Occurrences of the specified element: ============================================================ [1,2,3,4,1,2,3,4,1,1,2,2,3,3,4] ==> 1 ==> 4 ==> 2 ==> 4 ==> 3 ==> 4 ==> 4 ==> 3 count(): ======== 1) is an inbuilt method 2) use to count the number of occurrences of the specified element. Syntax: list-object.count(list-element) 3) count() can return an integer, which describe the number of occurrences, if the specified element in in the given list. 4) if the specified element is not in the given list: count() can return a zero. # count() listData = [1,2,3,4,1,1,2,2,3,3,4] cnt1 = listData.count(1) cnt2 = listData.count(2) cnt3 = listData.count(3) cnt4 = listData.count(4) cnt5 = listData.count(5) print(cnt1);print(cnt2);print(cnt3);print(cnt4);print(cnt5) ==================================== Find the first occurrence (index) of the element in the list: ============================================================= [1,2,3,4,1,2,3,4,4,3,2,1] 4 is at: 3rd index 7th index 8th index index(): ======== use to get the first occurrence/index (if the element is at multiple places) of the specified element in a list. Syntax: list-object.index(list-element) can return: an index value, if the specified element is in the given list can return a Value error:, if the specified element is not in the given list. index() can also use to find the element within the specified range also. Syntax: list-object.index(list-element, s-index, e-index) # index() ld = [1,2,3,4,4,3,2,1,1,2,3,4] index = ld.index(4) # i1 = ld.index(5) # i1 = ld.index(4,5,10) i1 = ld.index(2,4,10) print(index) print(i1) ==================================================== Adding of element at last in the list: ====================================== [1,2,3,4] add '5' at the end [1,2,3,4,5] append(): ========= 1) is an inbuilt method 2) can use to add an element at the end of the list Syntax: list-object.append(element/value) # WAP TO CREATE THE LIST WITH ONLY EVEN NUMBERS FROM 1 TO 30. # Solution-1: listData = list(range(2,30,2)) print("The list with only even integers = ",listData) # solution-2: ld = list() for i in range(1,30): if i % 2 == 0: ld.append(i) print("The List With only Even numbers is = ",ld) ===================================================== Insertion/Adding of an element to the list at the specified position/index: =========================================================================== [1,2,3,4] append(5) ==> [1,2,3,4,5] insert 5 ==> [1,2,5,3,4] insert(): ======== Syntax: list-data.insert(index/position, value/element) Returning value of insert() ==> None if the specified index is out of range: insert() can insert an element at the end by default. # insert() ld = eval(input("Enter a list:")) print("Before the insertion:") print(ld) print(ld.insert(3,97)) ld.insert(10,79) print("After the insertion:") print(ld) =============================================== EXTENDING OF ONE LIST DATA TO ANOTHER LIST DATA: ================================================ [1,2,3,4] [5,6,7,8] extending ==> [1,2,3,4,5,6,7,8] [5,6,7,8,1,2,3,4] extend() ======== Syntax: list-object1.extend(list-object2) # extend() ld1 = eval(input("Enter a list:")) ld2 = eval(input("Enter a list:")) print("Before the extend operation:") print(ld1) print(ld2) ld1.extend(ld2) print("After the extend Operation:") print(ld1) print(ld2) ============================================= Removal of specific element from the list: ========================================== remove(): ======== Syntax: list-object.remove(list-element) => remove() can return: None => if the specified element is not in the list: remove() can return: value Error # remove() ld = [1,3,5,7,9,10,30,50,70,90] print("The List Before the remove is = ") print(ld) print(ld.remove(30)) # print(ld.remove(100)) print("The List After the remove is = ") print(ld) Note: ==== If the specified element is duplicated: remove() can remove the value from first occurrence/index. =============================================== Removing the Last element from the list: ======================================== pop(): ===== Syntax: list-object.pop() 1) if a list is an empty: pop() can return: "Index Error" 2) if a list with something: pop() can remove the last element, by default. 3) pop() can return the value which is deleting from the list. # pop() ld = [] ld1 = [1,3,5,7,9,11,13,15] print(ld);print(ld1) # ld.pop() # print(ld) print(ld1.pop()) ld1.pop() print(ld1) ================================== del property: ============= which we can use to delete the whole data or the individual data from collection. to delete the specific element from list: Syntax: del list-data[index] to delete the whole list: Syntax: del list-data-name Note: ==== after the delete operation, if the list data is accessing: del property ==> name error. # del property ld = eval(input("Enter a list")) print("The List Before delete operation:") print(ld) del ld[3] print("The List After the delete operation:") print(ld) del ld # print(ld) ================================================ Clearing of the whole list: =========================== clear() ======= Syntax: list-data.clear() # clear() ld = eval(input("Enter a list:")) print("The List before the clear operation is:") print(ld) ld.clear() print("The List after the clear operation is:") print(ld)