-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path9_Lists and Lists Functions.py
167 lines (112 loc) · 5.65 KB
/
9_Lists and Lists Functions.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
'''
1. Lists - [ ]
a) Python lists are containers used to store a list of values of any data type.
b) We can also say that it is a collection of elements from any data type.
c) Special Properties of Lists :
* List is a collection which is :-
* ordered.
* changeable.
* Allows duplicate members.
2. Indexing of Lists -
a) List elements can also be accessed by using Indices, i.e. the first element has 0 and so on.
b) If you put an index that isn't on the list, you will get an error.
3. Slicing of Lists -
a) Like string slicing, we can also do slicing of Lists. Also the default value of step argument is 1 (same as in string).
slice1 = list1[start_index : stop_index : step]
b) NOTE -
* Never put any negative number smaller than -1 in step argument while slicing. eg:- -2, -3, -4 should never be used in Lists and strings. It doesn't works for some reason. (Could be supported in newer versions.)
* The default value of 'start_index' is always 0. Likewise, the default value of 'stop_index' is always the length of the list or string.
4. List Methods -
Some mostly used methods.
a) list.append()
b) list.insert()
c) list.remove()
d) list.pop()
e) list.sort()
5) All Lists Methods -
Method Description
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count() Returns the number of elements with the specified value.
extend() Add the elements of a list (or any iterable), to the end of the current list.
index() Returns the index of the first element with the specified value.
insert() Adds an element at the specified position
pop() Removes the element at the specified position
remove() Removes the item with the specified value
reverse() Reverses the order of the list
sort() Sorts the list
'''
#Examples :-
lists1 = ["Hello","Kumiko",True,456,334.98,"Olof","Teddy",False,None,546,87,"Puppy"]
lists2 = [454,64,54,87,64,2,5,6,84,6,43,21,84,354,846,354465,431,654687,6843,676,3,44,6,6,6]
#Some important Built-in functions -
print(len(lists1))
print(type(lists1))
print(max(str(lists1)))
print(min(str(lists1)))
print(max(lists2))
print(min(lists2))
#Indexing & Slicing
print(lists1[::])
print(lists1[0:12:1]) #Using default values of start_index, stop_index and step. start_index - 0, stop_index - length of the list, index - 1.
print(lists1[::-1]) #reverse the list
print(lists1[::-3]) #Using -ve step value smaller than -1 gives appropriate results as there are no values for arguments for the start_index and stop_index.
print(lists1[0:12:-3]) #Using the -ve step value smaller than -1 will result in an unexpected behavior as there are values for arguments of start_index & stop_index.
print(lists1[2:9:-1])
#Lists Methods -
#count()
print('\nThis is the count() method and the result is',lists2.count(6)) #count()
#extend() - here lists2 extends lists1
print(lists1.extend(lists2))
print('\nThis is the extend() method and the result is',lists1)
#append()
lists1.append('This is the appended item')
print('\nThis is the append() method and the result is',lists1)
#index()
print('\nThis is the index() method and result is',lists2.index(21))
#pop() - We put index number as an argument. Here 'Hello' having index 0 is popped and removed.
lists1.pop(0)
print('\nThis is the pop() method and the result is',lists1)
#remove() - We put the item-name as it is. Here None from list1 is removed.
lists1.remove(None)
print('\nThis is the remove() method and the result is',lists1)
#reverse()- reverse
lists1.reverse()
print('\nThis is the reverse() method and the result is',lists1)
#sort() - method
lists2.sort()
print('\nThis is the sort() method and the result is', lists2)
#O/P - Output
'''
12
<class 'list'>
y
654687
2
['Hello', 'Kumiko', True, 456, 334.98, 'Olof', 'Teddy', False, None, 546, 87, 'Puppy']
['Hello', 'Kumiko', True, 456, 334.98, 'Olof', 'Teddy', False, None, 546, 87, 'Puppy']
['Puppy', 87, 546, None, False, 'Teddy', 'Olof', 334.98, 456, True, 'Kumiko', 'Hello']
['Puppy', None, 'Olof', True]
[]
[]
This is the count() method and the result is 5
None
This is the extend() method and the result is ['Hello', 'Kumiko', True, 456,
334.98, 'Olof', 'Teddy', False, None, 546, 87, 'Puppy', 454, 64, 54, 87, 64,
2, 5, 6, 84, 6, 43, 21, 84, 354, 846, 354465, 431, 654687, 6843, 676, 3, 44,
6, 6, 6]
This is the append() method and the result is ['Hello', 'Kumiko', True, 456,
334.98, 'Olof', 'Teddy', False, None, 546, 87, 'Puppy', 454, 64, 54, 87, 64,
2, 5, 6, 84, 6, 43, 21, 84, 354, 846, 354465, 431, 654687, 6843, 676, 3, 44,
6, 6, 6, 'This is the appended item']
This is the index() method and result is 11
This is the pop() method and the result is ['Kumiko', True, 456, 334.98, 'Olof', 'Teddy', False, None, 546, 87, 'Puppy', 454, 64, 54, 87, 64, 2, 5, 6, 84, 6, 43, 21, 84, 354, 846, 354465, 431, 654687, 6843, 676, 3, 44, 6, 6, 6, 'This is the appended item']
This is the remove() method and the result is ['Kumiko', True, 456, 334.98, 'Olof', 'Teddy', False, 546, 87, 'Puppy', 454, 64, 54, 87, 64, 2, 5, 6, 84, 6, 43, 21, 84, 354, 846, 354465, 431, 654687, 6843, 676, 3, 44, 6, 6, 6, 'This
is the appended item']
This is the reverse() method and the result is ['This is the appended item',
6, 6, 6, 44, 3, 676, 6843, 654687, 431, 354465, 846, 354, 84, 21, 43, 6, 84,
6, 5, 2, 64, 87, 54, 64, 454, 'Puppy', 87, 546, False, 'Teddy', 'Olof', 334.98, 456, True, 'Kumiko']
This is the sort() method and the result is [2, 3, 5, 6, 6, 6, 6, 6, 21, 43,
44, 54, 64, 64, 84, 84, 87, 354, 431, 454, 676, 846, 6843, 354465, 654687]
'''