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