LIST OPERATIONS ================ DELETION OF LIST DATA: ===================== 4) clear() ======= ==> use to clear/delete the entire list Syntax: list_data/list_object.clear() ld = [1,3,5,7,9] print(ld) ld.clear() print(ld) ======================================== Reversing of list ============= reverse() ======= Syntax: list_data/list_object.reverse() ex: [1,2,3,4] reverse() ==> [4,3,2,1] # reversing of list ld = eval(input("Enter the list data:")) print("The List before the reverse operation is = ",ld) ld.reverse() print("The List after the reverse operation is = ",ld) ========================================= Sorting of list data: =============== Sorting ==> arranging the list elements from lower to upper value ==> Ascending order arranging of list elements in decreasing order sort() ==== Syntax: (for forward sorting) list_data/list_object.sort() ==> can use to arrange the list items in ascending order only. Syntax: (for reverse sorting) list_data/list_object.sort(reverse = True) # Forward sorting ===> ascending order arranging ld = [100,1,10,7,97,79,2,6] print("Before the sorting, the list = ") print(ld) ld.sort() print("After the sorting, the list = ") print(ld) # reverse sorting ==> descending order arrangement ld = [100,1,10,7,97,79,2,6] print("Before the sorting, the list = ") print(ld) ld.sort(reverse = True) print("After the sorting, the list = ") print(ld) ======================================= List Aliasing and Cloning =================== when the same data with different names, all the names can pointed with same address. ex: a = 10;b = 10;c = 10; id(a);id(b);id(c) ==> same address List Aliasing ========= when the same list data, represented with two or more names all the names(variables) pointed with the same address. ==> here, the list variables need to create from other list using 'assignment operator' Note: when the same list data to different variables with different definitions, the addresses of all the variables ==> different "When a list can define from another list using assignment operator, here: both the lists can pointed with same addresses. When any change on any list in this, it will reflect on both. This is called as "list aliasing" List Cloning: ========= copy() ==== ==> use to create a list from another list but with the different object address. list_data2 = lsit_data1.copy() # List Aliasing l1 = [1,3,5,7,9] l2 = [1,3,5,7,9] l3 = [1,3,5,7,9] l4 = l1 l5 = l1.copy() print(id(l1),id(l2),id(l3)) print(id(l4)) print(id(l5)) print(l1);print(l2);print(l3);print(l4);print(l5) print() l1[4] = 97 print(l1);print(l2);print(l3);print(l4);print(l5) print() l4[0] = 79 l5[1] = 97879 print(l1);print(l2);print(l3);print(l4);print(l5) print() ================================================ Math Operations ============= 1) List Concatenation ================= Joining of two or more lists into one ==> list concatenation + symbol Syntax: list_data1 + list_data2 + list_data3 2) List Repitition ============= To make repeat the list items for several number of times, use list repitition symbol: * Syntax: list_data * n.times # Math Operations l1 = [1,3,5,7,9] l2 = [0,1,2,3,4] print("Concatenated List = ",l1+l2) print("List with repetition is = ",l1*3) ============================ Nested List ========= nld = [[1,2,3,4],[5,6,7,8,],[9,10,11,12],[13,14,15,16]] print(nld[0]) print(nld[1]) print(nld[2]) print(nld[3]) print(nld[0][0]) print(nld[0][1]) print(nld[0][2]) print(nld[0][3]) for i in nld: print(i) print() # Wap to take a list in list (nested list) and print the list data as matrix formaT. for j in nld: for k in j: print(k,end = "\t") print()