|
58 | 58 | In the case of multiple occurrences of the same element, only the first occurrence is removed.
|
59 | 59 |
|
60 | 60 | EXAMPLE:
|
| 61 | + list1 = [1,2,3,4,5] |
| 62 | + list1.remove(5) |
| 63 | + print(list1) |
| 64 | + |
| 65 | + OUTPUT: |
| 66 | + [1,2,3,4] |
| 67 | + |
| 68 | +5. pop() |
| 69 | + The method pop() can remove an element from any position in the list. |
| 70 | + The parameter supplied to this method is the index of the element to be removed. |
| 71 | + |
| 72 | + EXAMPLE: |
| 73 | + list1 = [1,2,3,4,5] |
| 74 | + list1.pop() # if the index in not specified then the last element will be deleted |
| 75 | + list1.pop(0) # this deletes the element at 0th index |
| 76 | + |
| 77 | + OUTPUT: |
| 78 | + [2,3,4] |
| 79 | + |
| 80 | +6. len() |
| 81 | + The len() method returns the length of the list, i.e. the number of elements in the list. |
| 82 | + |
| 83 | + EXAMPLE: |
| 84 | + list1 = [1,2,3,4,5] |
| 85 | + print(len(list1)) |
| 86 | + |
| 87 | + OUTUPT: |
| 88 | + 5 |
| 89 | + |
| 90 | +7. min() & max() |
| 91 | + The min() method returns the minimum value in the list. |
| 92 | + The max() method returns the maximum value in the list. |
| 93 | + Both the methods accept only homogeneous lists, i.e. list having elements of similar type. |
| 94 | + |
| 95 | + EXAMPLE: |
| 96 | + list = [1,2,3,4,5] |
| 97 | + print(min(list)) |
| 98 | + print(max(list)) |
| 99 | + |
| 100 | + OUTPUT: |
| 101 | + 1 #minimum of the list |
| 102 | + 5 #maximum of the list |
| 103 | + |
| 104 | +8. sort() |
| 105 | + The sort method sorts the list in ascending order. |
| 106 | + This operation can only be performed on homogeneous lists, i.e. lists having elements of similar type |
| 107 | + SORT METHOD MAKE CHANGES TO THE ORIGINAL LIST |
| 108 | + |
| 109 | + EXAMPLE: |
| 110 | + list1 = [5,3,6,23,2] |
| 111 | + list1.sort() |
| 112 | + print(list1) |
| 113 | + |
| 114 | + OUTPUT: |
| 115 | + [2,3,5,6,23] |
| 116 | + |
| 117 | +9.sorted() |
| 118 | + The sort method sorts the list in ascending order. |
| 119 | + This operation can only be performed on homogeneous lists, i.e. lists having elements of similar type |
| 120 | + UNLIKE SORT , SORTED METHOD RETURNS A NEW LIST AND THE ORIGINAL LIST IS NOT CHANGED |
| 121 | + |
| 122 | + EXAMPLE: |
| 123 | + list1 = [5,3,6,23,2] |
| 124 | + l1 = sorted(list1) |
| 125 | + print(list1) |
| 126 | + print(l1) |
| 127 | + |
| 128 | + OUTPUT: |
| 129 | + [5,3,6,23,2] #original list |
| 130 | + [2,3,5,6,23] #new sorted list |
| 131 | + |
| 132 | +10. clear() |
| 133 | + clear method is used to delete all the elements in the list , but the list in not deleted |
| 134 | + |
| 135 | + EXAPMLE: |
| 136 | + list1 = [2,34,5,6,3] |
| 137 | + list1.clear() |
| 138 | + print(list1) |
| 139 | + |
| 140 | + OUTPUT: |
| 141 | + [] |
| 142 | + |
| 143 | +11. list() |
| 144 | + 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 |
| 145 | + |
| 146 | + EXAMPLE: |
| 147 | + tu = (1,2,3,4) |
| 148 | + st = {1,2,3,4} |
| 149 | + l1 = list(tu) |
| 150 | + l2 = list(st) |
| 151 | + print(l1) |
| 152 | + print(l2) |
| 153 | + |
| 154 | + OUTPUT: |
| 155 | + [1,2,3,4] |
| 156 | + [1,2,3,4] |
| 157 | + |
| 158 | +12. tuple() |
| 159 | + 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 |
| 160 | + |
| 161 | + EXAPMLE: |
| 162 | + li = [1,2,3,4] |
| 163 | + st = {1,2,3,4} |
| 164 | + t1 = tuple(l1) |
| 165 | + t2 = tuple(l2) |
| 166 | + print(t1) |
| 167 | + print(t2) |
| 168 | + |
| 169 | + OUTPUT: |
| 170 | + (1,2,3,4) |
| 171 | + (1,2,3,4) |
| 172 | + |
| 173 | + |
| 174 | + |
61 | 175 |
|
| 176 | + |
0 commit comments