1
+ '''
2
+ LISTS
3
+ A list is a data structure in Python that is a mutable, or changeable, ordered sequence of elements
4
+
5
+ Advantage of List over Linked List:
6
+ Elements can be accessed through their index value
7
+
8
+ BigO of Array:
9
+ Indexing -----> O(1)
10
+ Insert/Delete element at Start -----> O(n)
11
+ Insert/Delete element at End -----> O(1)
12
+ Insert element at Middle -----> O(n)
13
+ '''
14
+
15
+ # Empty List
16
+ list = []
17
+ print (f"This is an Empty List: { list } \n " )
18
+
19
+ # List with Elements
20
+ list = [1 , 2 , 3.1 , "A" ]
21
+ print (f"Elements of the list are: { list } \n " )
22
+
23
+
24
+ ''' Adding elements '''
25
+
26
+ print ("**********Adding Elements**********" , end = "\n \n " )
27
+
28
+ #add as a single element
29
+ list .append (["B" , 4 ])
30
+ print ("Appended List " , list , end = "\n \n " )
31
+
32
+ #add as different elements
33
+ list .extend ([5 , 'E' ])
34
+ print ("Entended List " , list , end = "\n \n " )
35
+
36
+ #add element at 1st Index
37
+ list .insert (0 , 0 )
38
+ list .insert (5 , "F" )
39
+ print ("Inserted Element " , list , end = "\n \n " )
40
+
41
+
42
+ ''' Deleting Elements '''
43
+
44
+ print ("**********Deleting Elements**********" , end = "\n \n " )
45
+ #delete element at index 5
46
+ del list [5 ]
47
+ print ("Deleted element " , list , end = "\n \n " )
48
+
49
+ #remove element with value '<val>'
50
+ list .remove ('E' )
51
+ print ("Removed element " , list , end = "\n \n " )
52
+
53
+ #pop element from list at i^th index
54
+ a = list .pop (1 )
55
+ print ('Popped Element: ' , a , ' List remaining: ' , list , end = "\n \n " )
56
+
57
+ #empty the list
58
+ list .clear ()
59
+ print ("Empty List " , list , end = "\n \n " )
60
+
61
+
62
+ ''' Accessing Elements '''
63
+
64
+ print ("**********Accessing Elements**********" , end = "\n \n " )
65
+
66
+ list = [1 , 2 , 3.5 , "A" , True ]
67
+ print ("This is new list " , list , end = "\n \n " )
68
+
69
+ #access elements one by one
70
+ print ("Accessing all Elements one by one\n " )
71
+ for element in list :
72
+ print (element , end = "\n " )
73
+
74
+ #access all elements
75
+ print ("\n Accessing all elements\n " )
76
+ print (list , end = "\n \n " )
77
+
78
+ #access index i element
79
+ print ("Element at index=2 is" , list [2 ], end = "\n \n " )
80
+
81
+ #access elements from 0 to 1 and exclude 2
82
+ print ("Elements from index(0 to 1) are " , list [0 :2 ], end = "\n \n " )
83
+
84
+ #access elements in reverse
85
+ print ("Elements in reverse order " , list [::- 1 ], end = "\n \n " )
86
+
87
+
88
+ '''
89
+ Other Functions
90
+ len() : returns length of the list.
91
+ index() : finds index value of value passed where it has been encountered the first time.
92
+ count() : finds count of the value passed to it.
93
+ sorted() and sort() : to sort the values of the list.
94
+ sorted() has a return type whereas the sort() modifies the original list.
95
+ '''
96
+
97
+ print ("**********Other Functions**********" , end = "\n \n " )
98
+
99
+ list = [1 , 2 , 3 , 10 , 20 , 50 ]
100
+
101
+ #find length of list
102
+ print ("No of elements in the list " , len (list ), end = "\n \n " )
103
+
104
+ #find index of element that occurs first
105
+ print ("Index of element 20 is " , list .index (20 ), end = "\n \n " )
106
+
107
+ #find count of the element
108
+ print (f"Elements has been repeated { list .count (10 )} time" , end = "\n \n " )
109
+
110
+ #print sorted list but not change original
111
+ print ("Sorted list is [AESC] " , sorted (list ), end = "\n \n " )
112
+
113
+ #sort original list
114
+ list .sort (reverse = True )
115
+ print ("Sorted list is [DESC]" , list , end = "\n \n " )
0 commit comments