-
Notifications
You must be signed in to change notification settings - Fork 1
/
list_methods_implementation.py
57 lines (41 loc) · 1.29 KB
/
list_methods_implementation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'''
Author: Davide Pollicno
Date: 27/01/2020
Summary: Implementation of the most widely used API of python list
'''
# Built-in method: clear() -> It removes all the element from the list
def clear_method(array):
del array[:]
return array
def display(array):
for el in array:
print(el , end=" ")
# Built-in method: len() -> returns the length / number of elements in a list
def length(array):
counter = 0
for i in array:
counter += 1
return counter
# Built-in method: count() -> It returns the number of elements in a list
def count_method(array):
return len(array)
# Built-in method: append() -> It add a new element at the last position of the list.
def append_method(array , element_to_add):
array[len(array):] = [element_to_add]
# Starting point of the program
def main():
array = []
append_method(array , 5)
append_method(array , 3)
append_method(array , 450)
append_method(array , 66)
append_method(array , 12)
print("Number of elements in the array: {0}".format(count_method(array)))
print("Array content: ")
display(array)
print("\nLength of the array: {0}".format(length(array)))
clear_method(array)
print("\nArray content after clear method: ")
display(array)
if __name__ == "__main__":
main()