-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlists.py
97 lines (76 loc) · 3.07 KB
/
lists.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# lists functions
names = ["rob", "bob", "shaw", "newton", "jack", "bill"]
no = [5, 16, 8, 12, 4, 32, 1]
# extend function.
# allow a list to append onto a list
names.extend(no)
print(names)
# OUTPUT: ['rob', 'bob', 'shaw', 'newton', 'jack', 'bill', 5, 16, 8, 12, 4, 32, 1]
# ----------------------------------------------------------------------------------------
# add individual element onto a list
names = ["rob", "bob", "shaw", "newton", "jack", "bill"]
no = [5, 16, 8, 12, 4, 32, 1]
names.append("noah")
print(names)
# OUTPUT:['rob', 'bob', 'shaw', 'newton', 'jack', 'bill', 'noah']
# --------------------------------------------------------------------------------------
# insert at a index point
names = ["rob", "bob", "shaw", "newton", "jack", "bill"]
no = [5, 16, 8, 12, 4, 32, 1]
names.insert(2,"shaun")
print(names)
# OUTPUT:['rob', 'bob', 'shaun', 'shaw', 'newton', 'jack', 'bill']
# -------------------------------------------------------------------------------------------
# remove elements
names = ["rob", "bob", "shaw", "newton", "jack", "bill"]
no = [5, 16, 8, 12, 4, 32, 1]
names.remove("jack")
print(names)
# OUTPUT:['rob', 'bob', 'shaw', 'newton', 'bill']
# remove all the elements in the list
names = ["rob", "bob", "shaw", "newton", "jack", "bill"]
no = [5, 16, 8, 12, 4, 32, 1]
names.clear()
print(names)
# OUTPUT: []
# pop element
names = ["rob", "bob", "shaw", "newton", "jack", "bill"]
no = [5, 16, 8, 12, 4, 32, 1]
names.pop() #removes the last element.
print(names)
# OUTPUT:['rob', 'bob', 'shaw', 'newton', 'jack']
# ---------------------------------------------------------------------------------------------
# index function
names = ["rob", "bob", "shaw", "newton", "jack", "bill"]
no = [5, 16, 8, 12, 4, 32, 1]
print(names.index("bob"))
# returns the index value.
# OUTPUT:1
# ---------------------------------------------------------------------------------------------
# count function to check if duplicate element is present'
names = ["rob", "bob", "shaw", "newton", "jack", "bill"]
no = [5, 16, 8, 12, 4, 32, 1]
names = ["rob", "bob", "bob", "shaw", "newton", "jack", "bill"]
print(names.count("bob")) #counts how many times bob as repeated in the list
# OUTPUT:2
# ----------------------------------------------------------------------------------------------
# sort in alphabetical order
names = ["rob", "bob", "shaw", "newton", "jack", "bill"]
no = [5, 16, 8, 12, 4, 32, 1]
names.sort()
print(names)
no.sort()
print(no)
# OUTPUT: ['bill', 'bob', 'jack', 'newton', 'rob', 'shaw']
# OUTPUT: [1, 4, 5, 8, 12, 16, 32]
# ------------------------------------------------------------------------------------------------
# copy function
names = ["rob", "bob", "shaw", "newton", "jack", "bill"]
no = [5, 16, 8, 12, 4, 32, 1]
newnames = names.copy()
print(newnames)
newno = no.copy()
print(newno)
# OUTPUT: ['rob', 'bob', 'shaw', 'newton', 'jack', 'bill']
# [5, 16, 8, 12, 4, 32, 1]
# ------------------------------------------------------------------------------------------------------------