Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. >>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  2. >>> del a[-1]
  3. >>> a
  4. [0, 1, 2, 3, 4, 5, 6, 7, 8]
  5.  
  6. >>> del a[2:4]
  7. >>> a
  8. [0, 1, 4, 5, 6, 7, 8, 9]
  9.  
  10. a = ['a', 'b', 'c', 'd']
  11. a.pop(1)
  12.  
  13. # now a is ['a', 'c', 'd']
  14.  
  15. a = ['a', 'b', 'c', 'd']
  16. a.pop()
  17.  
  18. # now a is ['a', 'b', 'c']
  19.  
  20. >>> a = [1, 2, 3, 4, 5, 6]
  21. >>> index = 3 # Only positive index
  22.  
  23. >>> a = a[:index] + a[index+1 :]
  24. # a is now [1, 2, 3, 5, 6]
  25.  
  26. class foo(object):
  27. def __init__(self, items):
  28. self.items = items
  29.  
  30. def __getitem__(self, index):
  31. return foo(self.items[index])
  32.  
  33. def __add__(self, right):
  34. return foo( self.items + right.items )
  35.  
  36. a = range(10)
  37. index = 3
  38.  
  39. def del_method():
  40. global a
  41. global index
  42. del a[index]
  43.  
  44. 10 0 LOAD_GLOBAL 0 (a)
  45. 3 LOAD_GLOBAL 1 (index)
  46. 6 DELETE_SUBSCR # This is the line that deletes the item
  47. 7 LOAD_CONST 0 (None)
  48. 10 RETURN_VALUE
  49. None
  50.  
  51. def pop_method():
  52. global a
  53. global index
  54. a.pop(index)
  55.  
  56. 17 0 LOAD_GLOBAL 0 (a)
  57. 3 LOAD_ATTR 1 (pop)
  58. 6 LOAD_GLOBAL 2 (index)
  59. 9 CALL_FUNCTION 1
  60. 12 POP_TOP
  61. 13 LOAD_CONST 0 (None)
  62. 16 RETURN_VALUE
  63.  
  64. def slice_method():
  65. global a
  66. global index
  67. a = a[:index] + a[index+1:]
  68.  
  69. 24 0 LOAD_GLOBAL 0 (a)
  70. 3 LOAD_GLOBAL 1 (index)
  71. 6 SLICE+2
  72. 7 LOAD_GLOBAL 0 (a)
  73. 10 LOAD_GLOBAL 1 (index)
  74. 13 LOAD_CONST 1 (1)
  75. 16 BINARY_ADD
  76. 17 SLICE+1
  77. 18 BINARY_ADD
  78. 19 STORE_GLOBAL 0 (a)
  79. 22 LOAD_CONST 0 (None)
  80. 25 RETURN_VALUE
  81. None
  82.  
  83. >>> x = [1, 2, 3, 4]
  84.  
  85. >>> p = x.pop(1)
  86. >>> p
  87. 2
  88.  
  89. >>> myList = [10,20,30,40,50]
  90. >>> rmovIndxNo = 3
  91. >>> del myList[rmovIndxNo]
  92. >>> myList
  93. [10, 20, 30, 50]
  94.  
  95. >>> l = [1, 2, 3, 4, 5]
  96. >>> l.pop(2)
  97. 3
  98. >>> l
  99. [1, 2, 4, 5]
  100.  
  101. >>> l = [1, 2, 3, 4, 5]
  102. >>> del l[2]
  103. >>> l
  104. [1, 2, 4, 5]
  105.  
  106. a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  107.  
  108. # remove the element at index 3
  109. a[3:4] = []
  110. # a is now [0, 1, 2, 4, 5, 6, 7, 8, 9]
  111.  
  112. # remove the elements from index 3 to index 6
  113. a[3:7] = []
  114. # a is now [0, 1, 2, 7, 8, 9]
  115.  
  116. letters = ["a", "b", "c", "d", "e"]
  117. letters.remove(letters[1])
  118. print(*letters) # Used with a * to make it unpack you don't have to (Python 3.x or newer)
  119.  
  120. del my_list[2]
  121. del my_list[3]
  122. del my_list[7]
  123.  
  124. new list = [j for i, j in enumerate(my_list) if i not in [2, 3, 7]]
  125.  
  126. list = [1, 2, 3, 4]
  127. list.remove(1)
  128. print(list)
  129.  
  130. output = [2, 3, 4]
  131.  
  132. list = [1, 2, 3, 4]
  133. list.remove(list[2])
  134. print(list)
  135. output : [1, 2, 4]
  136.  
  137. a = ['a', 'b', 'c', 'd']
  138.  
  139. def remove_element(list_,index_):
  140. clipboard = []
  141. for i in range(len(list_)):
  142. if i is not index_:
  143. clipboard.append(list_[i])
  144. return clipboard
  145.  
  146. print(remove_element(a,2))
  147.  
  148. >> ['a', 'b', 'd']
  149.  
  150. if index_<0:index_=len(list_)+index_
  151.  
  152. >>> test = ['item1', 'item2']
  153. >>> test.pop(-1)
  154. 'item2'
  155. >>> test
  156. ['item1']
  157.  
  158. l = range(20)
  159. inds2rem = [2,5,1,7]
  160. map(lambda x: l.pop(x), sorted(inds2rem, key = lambda x:-x))
  161.  
  162. >>> l
  163. [0, 3, 4, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
  164.  
  165. del listName[-N]
  166.  
  167. del listName[-3:]
  168.  
  169. del listName[-8:]
  170.  
  171. print([v for i,v in enumerate(your_list) if i not in list_of_unwanted_indexes])
  172.  
  173. print([v for i,v in enumerate(your_list) if i != unwanted_index])
  174.  
  175. >>> l = [1,2,3,4,5]
  176. >>> del l[-1:]
  177. >>> l
  178. [1, 2, 3, 4]
  179. >>> l.pop(-1)
  180. 4
  181. >>> l
  182. [1, 2, 3]
  183.  
  184. >>> a=[1,2,3,4,5]
  185. >>> del a[1]
  186. >>> a
  187. [1, 3, 4, 5]
  188. >>> a.pop(1)
  189. 3
  190. >>> a
  191. [1, 4, 5]
  192. >>>
  193.  
  194. >>> l = [0,1,2,3,4,5,6,7,8,9]
  195. >>> indices=[3,7]
  196. >>> for i in indices:
  197. ... del l[i]
  198. ...
  199. >>> l
  200. [0, 1, 2, 4, 5, 6, 7, 9]
  201.  
  202. >>> l = [0,1,2,3,4,5,6,7,8,9]
  203. >>> indices=[3,7]
  204. >>> for i in sorted(indices, reverse=True):
  205. ... del l[i]
  206. ...
  207. >>> l
  208. [0, 1, 2, 4, 5, 6, 8, 9]
  209.  
  210. v = [1, 2, 3, 4, 5, 6]
  211. v.remove(v[4]) # I'm removing the number with index 4 of my array
  212. print(v) # If you want verify the process
  213.  
  214. # It gave me this:
  215. #[1,2,3,4,6]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement