List Manipulations ================== append(): ======== -> which can use to add element at the end of the list. Syntax: list-object-name.append(value) insert(): ======== -> can use to add the element at the specified index/position. Syntax: list-object-name.insert(index, value) -> insert() behave like append() also, that means insert() can add value/element at last of the list as like append() when the specified index is out of range. # List is a collection datatype # list is non-primitive datatype # list data can define with [] and elements in [] must be separate with comma # List have a built-in class ==> "list" # List is mutable object a = [] # emty list print("Before the modification:") print(a) print(type(a)) print("address of list 'a' = ",id(a)) # adding of elements a.append(100) a.append(201) a.append(301) print("The List 'a' after the modification:") print(a) print("address of list 'a' is = ",id(a)) # list is index supposrted print(a[0],a[1],a[2]) print(a[-1],a[-2],a[-3]) # insertion a.insert(2,123) print(a,id(a)) a.insert(40,121) print(a) # list allows the slicing print("The given list = ",a[::1]) print("The Reverse of the list = ",a[::-1]) # list can be homogeneous as like array # and also define with heterogeneous elements b = ["String",102,1e-3,12-23j,True] print(type(b)) # list is ordered datatype print(b) ======================================================== Dynamic List: ============= eval() ====== Syntax: list-object-name = eval(input()) a = eval(input("Enter a list:")) print(a) print(type(a)) ============================================= Looping on list: ================ while ===== a = eval(input("Enter a list:")) print("The List elements = ") index = 0 while index < len(a): print(a[index]) index = index + 1 ================= for loop: ========= Syntax: for iteration-variable in list-name: //loop body a = eval(input("Enter a list:")) print("The List elements = ") for i in a: print(i) ============================================= Math Operations: ================ 1) List Concatenation: ====================== -> Joining of list elements at the at of the previous list Syntax: l1 + l2 + l3 + l4 + ... 2) List Repetition: =================== -> if you want to repeat the list elements for several times, we can use "list repetition". Syntax: list-data * n ======================================================== How to create a new list from another list: =========================================== 1) using assignment operator =========================== l1 = [12,24,36,48,60] l2 = l1 print(l1) print(l2) print(id(l1)) print(id(l2)) l2.append(72) print(l1) print(l2) # list aliaisng: when a change on one list object can reflect another list object also this is called # as "list aliasing" # list aliasing is possible with'=' cloning ======= -> list cloning is defined as: a list which can be created from another list the change on one list cannot reflect on another. 2) using copy() =============== 3) using slicing ================ # list cloning a = [11,22,33,44,55,66,77,88] b = a.copy() print(a) print(b) print(id(a)) print(id(b)) b.append(101) print(a) print(b) c = a[:] print(a) print(c) print(id(a)) print(id(c)) a.insert(2,101) print(a) print(c) 4) using extend() ================= Syntax: l1.extend(l2) l1 = [1,3,5,7,9] l2 = ['a','e','i','o','u'] print(l1) print(l2) l1.extend(l2) print(l1) print(l2) ============================================= reverse() ========= -> can use to reverse the list Syntax: list-data-name.reverse() l1 = [1,3,5,7,9] print(l1) l1.reverse() print(l1) ========================================== sorting of the list: ==================== sort() ====== 1) Natural sorting ==> ascending order/Forward sorting ======================================================= Syntax: list-data-name.sort() 2) Unnatural sorting ==> descending order/Reverse Sorting ========================================================= Syntax: list-data-name.sort(reverse = True) l1 = [101,1,99,-10,0,97,79,3,-1] print("The original list without sorting is = ",l1) # natural sorting/Forward sorting l1.sort() print("The List after the sorting is = ",l1) # unnatural sorting/Reverse Sorting l1.sort(reverse = True) print("The List after the sorting is = ",l1) ====================================================== Remove Operations: ================== pop() ===== -> this method can use to remove the last element from the list. Syntax: list-data-name.pop() remove() ======== -> this method can use to remove the specified element from the list. Syntax: list-data-name.remove(element) here: while removing the element, remove() can return "None". -> remove() can return "value error" when the specified element is not present in the given list. del operator ============ -> del is an operator can delete an element or list object permanently. Syntax: del list-name[index] ==> can delete the specified element del list-name ==> can delete the whole list permanently clear() ======= -> can make the list as empty by removing all elements from the list. Syntax: list-data.clear() l1 = [1,3,5,7,9,2,0,4,6,10,8] print("The Given list = ",l1) # pop() l1.pop() print("The list after the pop operation is = ",l1) # remove() x = l1.remove(0) print("The List after the remove operation is = ",l1) print(x) # l1.remove(100) # print("The List after the remove operation is = ",l1) del l1[3] print(l1) # del l1 # print(l1) l1.clear() print(l1)