LIST DATA STRUCTURE ==================== LIST AND ITS FEATURES/PROPERTIES ================================ 1) A SEQUENTIAL COLLECTION DATA ITEM 2) CAN DEFINE WITH [] AND ALL ELEMENTS IN [] MUST BE SEPARATED WITH COMMA. EX: [ELE1,ELE2,.....] 3) PRE-DEFINED CLASS: LIST BECAUSE, THE LIST DATATYPE IS AN INBUILT DATATYPE. 4) List is Sequenced because the list data can be get accessed with indexing (positive indexing/negative indexing) 5) List is supposed to accessing the part of the list using slicing listData = [] # empty list listData1 = [1,3,5,7,9,11] print(type(listData)) print(type(listData1)) # positive indexing ==> forward accessing print(listData1[0]) print(listData1[1]) print(listData1[2]) print(listData1[3]) print(listData1[4]) print(listData1[5]) print() # negative indexing ==> reverse accessing print(listData1[-1]) print(listData1[-2]) print(listData1[-3]) print(listData1[-4]) print(listData1[-5]) print(listData1[-6]) # slicing print(listData1[0:6]) print(listData1[0:6:2]) print(listData1[-1:-7:-1]) print(listData1[::1]) print(listData1[::-1]) 6) List can possible to define with same type of data items ==> Homogeneous List 7) List can possible to define with different type of data items ==> Heterogeneous list 8) List can ordered. 9) List can possible to define with duplicated elements. # Homogenous List li = [1,2,3,4,5,6,7,8,9,10] # list with integers lf = [0.1,0.2,0.3,0.4,0.5] # list with floats lc = [1-2j,1+2j,10-20j,10+20j] # list with complex lb = [True, False, True, True, False] # list with booleans ls = ['a','b','c','d'] # list with strings print(type(li),type(lf),type(lc),type(lb),type(ls)) # Heterogeneous List lh = [111,123,0.001,1.2e-7,True, 12-2j, 'abcde'] ld = [10,20,30,30,20,10,11,22,33,10,22,33,44,100,10,20,11,22] print(type(lh)) print(type(ld)) print(li) print(lf) print(lc) print(lb) print(ls) print(lh) print(ld) ================================ HOW TO DEFINE THE LIST ====================== 1) Compile Time Definition ===================== Syntax: Identifier = [e1,e2,e3,...] 2) Run time definition ================= eval() ==== ==> an inbuilt method used to define any collection in the run time Syntax: eval(input()) lr = eval(input("Enter the list data:")) print(type(lr)) print("The Given List = ") print(lr) 3) Using list() ========== Syntax: identifier = list() ==> we can create an empty list ==> list() can create a list from other collections list() can do type conversion other collect ==> list collection Syntax: identifier = list(collection data) 4) split() ======= strData = "Python is High Level Programming Language" l1 = list() l2 = list("Python") l3 = list((1,2,3,4,5)) l4 = list({1,10,100,1000}) l5 = list({'a':10,'b':20,'c':30}) l6 = strData.split() print(type(l1)) print(l1) print(l2) print(l3) print(l4) print(l5) print(l6) ===================================== TRAVERSING ON THE LIST ====================== While loop: ======== ld = eval(input("Enter a list data:")) l = len(ld) index = 0 while index < l: print("The Element at positive {} and at negative index {} is = {}".format(index,index-l,ld[index])) index += 1 for loop ====== ld = eval(input("Enter a list data:")) l = len(ld) index = 0 for i in ld: print("The List Element at positive index {} and at negative index {} is = {}".format(index,index-l,i)) index += 1 ========================= # WAP TO FIND THE AVERAGE OF THE LIST ELEMENTS ld = eval(input("Enter a list:")) s = 0 avg = 0 for i in ld: s += i avg = s/len(ld) print("The Sum of List Elements = ",s) print("The Average of List Elements = ",avg) =================================== Membership Check: =============== l1 = [1,3,5,7,9] print(1 in l1) print(100 not in l1) List Comparision ============== l1 = [1, 2, 3, 4, 5] l2 = [1, 2, 3, 4, 5] l3 = [2, 4, 6, 8] l4 = [2, 4, 6, 8, 10] l5 = [2,1,0,3,4] # equality """ 1) whether both the list datas with same length or not 2) if both list datas are with same length, then: individual element comparison should perform """ print(l1 == l2) # True print(l1 != l3) # True print(l1 == l4) # False # with <, >, <=, >= """ 1) not required to check with size 2) individual element comparison should be used until the satisfactory. """ print(l1 > l3) print(l1 < l4) print(l1 <= l5)