-
Notifications
You must be signed in to change notification settings - Fork 0
/
12 - LIST AND TUPLE OPERATIONS
176 lines (133 loc) · 4.39 KB
/
12 - LIST AND TUPLE OPERATIONS
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
168
169
170
171
172
173
174
175
A few of the basic list operations used in Python programming are
1.extend(),
2.insert(),
3.append(),
4.remove(),
5.pop(),
6.min()
7.max(),
8.sort(),
9.sorted()
10.index(),
11.clear(),
12.list()
13.tuple(), etc.
1. append()
The append() method is used to add elements at the end of the list. This method can only add a single element at a time.
EXAMPLE:
list1 = [1,2,3,4,5]
list1.append(6) #adding 6 at the end
list1.append(7) #adding 7 at the end
print(list1)
OUTPUT:
[1,2,3,4,5,6,7]
2. extend()
The extend() method is used to add more than one element at the end of the list.
Although it can add more than one element, unlike append(), it adds them at the end of the list like append()
EXAMPLE:
list1 = [1,2,3,4,5]
list1.extend([8,9,10]) #adding 8,9,10 at the end, append use to add single element , extend is use to add multilple elements at the end
print(list1)
OUTPUT:
[1,2,3,4,5,8,9,10]
3. insert()
The insert() method can add an element at a given position in the list.
Thus, unlike append(), it can add elements at any position, but like append(),
it can add only one element at a time. This method takes two arguments.
The first argument specifies the position, and the second argument specifies the element to be inserted.
EXAMPLE:
list1 = [1,2,3,4,5]
list1.insert(0, -100) adding -100 at the 0th index
print(lsit1)
OUTPUT:
[-100, 1,2,3,4,5]
4. remove()
The remove() method is used to remove an element from the list.
In the case of multiple occurrences of the same element, only the first occurrence is removed.
EXAMPLE:
list1 = [1,2,3,4,5]
list1.remove(5)
print(list1)
OUTPUT:
[1,2,3,4]
5. pop()
The method pop() can remove an element from any position in the list.
The parameter supplied to this method is the index of the element to be removed.
EXAMPLE:
list1 = [1,2,3,4,5]
list1.pop() # if the index in not specified then the last element will be deleted
list1.pop(0) # this deletes the element at 0th index
OUTPUT:
[2,3,4]
6. len()
The len() method returns the length of the list, i.e. the number of elements in the list.
EXAMPLE:
list1 = [1,2,3,4,5]
print(len(list1))
OUTUPT:
5
7. min() & max()
The min() method returns the minimum value in the list.
The max() method returns the maximum value in the list.
Both the methods accept only homogeneous lists, i.e. list having elements of similar type.
EXAMPLE:
list = [1,2,3,4,5]
print(min(list))
print(max(list))
OUTPUT:
1 #minimum of the list
5 #maximum of the list
8. sort()
The sort method sorts the list in ascending order.
This operation can only be performed on homogeneous lists, i.e. lists having elements of similar type
SORT METHOD MAKE CHANGES TO THE ORIGINAL LIST
EXAMPLE:
list1 = [5,3,6,23,2]
list1.sort()
print(list1)
OUTPUT:
[2,3,5,6,23]
9.sorted()
The sort method sorts the list in ascending order.
This operation can only be performed on homogeneous lists, i.e. lists having elements of similar type
UNLIKE SORT , SORTED METHOD RETURNS A NEW LIST AND THE ORIGINAL LIST IS NOT CHANGED
EXAMPLE:
list1 = [5,3,6,23,2]
l1 = sorted(list1)
print(list1)
print(l1)
OUTPUT:
[5,3,6,23,2] #original list
[2,3,5,6,23] #new sorted list
10. clear()
clear method is used to delete all the elements in the list , but the list in not deleted
EXAPMLE:
list1 = [2,34,5,6,3]
list1.clear()
print(list1)
OUTPUT:
[]
11. list()
this method is used to conver the give arguments into a list , we can convert a tuple in to a list, a set into a list
EXAMPLE:
tu = (1,2,3,4)
st = {1,2,3,4}
l1 = list(tu)
l2 = list(st)
print(l1)
print(l2)
OUTPUT:
[1,2,3,4]
[1,2,3,4]
12. tuple()
this method is used to conver the give arguments into a tuple , we can convert a list in to a tuple, a set into a tuple
EXAPMLE:
li = [1,2,3,4]
st = {1,2,3,4}
t1 = tuple(l1)
t2 = tuple(l2)
print(t1)
print(t2)
OUTPUT:
(1,2,3,4)
(1,2,3,4)