Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- >>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- >>> del a[-1]
- >>> a
- [0, 1, 2, 3, 4, 5, 6, 7, 8]
- >>> del a[2:4]
- >>> a
- [0, 1, 4, 5, 6, 7, 8, 9]
- a = ['a', 'b', 'c', 'd']
- a.pop(1)
- # now a is ['a', 'c', 'd']
- a = ['a', 'b', 'c', 'd']
- a.pop()
- # now a is ['a', 'b', 'c']
- >>> a = [1, 2, 3, 4, 5, 6]
- >>> index = 3 # Only positive index
- >>> a = a[:index] + a[index+1 :]
- # a is now [1, 2, 3, 5, 6]
- class foo(object):
- def __init__(self, items):
- self.items = items
- def __getitem__(self, index):
- return foo(self.items[index])
- def __add__(self, right):
- return foo( self.items + right.items )
- a = range(10)
- index = 3
- def del_method():
- global a
- global index
- del a[index]
- 10 0 LOAD_GLOBAL 0 (a)
- 3 LOAD_GLOBAL 1 (index)
- 6 DELETE_SUBSCR # This is the line that deletes the item
- 7 LOAD_CONST 0 (None)
- 10 RETURN_VALUE
- None
- def pop_method():
- global a
- global index
- a.pop(index)
- 17 0 LOAD_GLOBAL 0 (a)
- 3 LOAD_ATTR 1 (pop)
- 6 LOAD_GLOBAL 2 (index)
- 9 CALL_FUNCTION 1
- 12 POP_TOP
- 13 LOAD_CONST 0 (None)
- 16 RETURN_VALUE
- def slice_method():
- global a
- global index
- a = a[:index] + a[index+1:]
- 24 0 LOAD_GLOBAL 0 (a)
- 3 LOAD_GLOBAL 1 (index)
- 6 SLICE+2
- 7 LOAD_GLOBAL 0 (a)
- 10 LOAD_GLOBAL 1 (index)
- 13 LOAD_CONST 1 (1)
- 16 BINARY_ADD
- 17 SLICE+1
- 18 BINARY_ADD
- 19 STORE_GLOBAL 0 (a)
- 22 LOAD_CONST 0 (None)
- 25 RETURN_VALUE
- None
- >>> x = [1, 2, 3, 4]
- >>> p = x.pop(1)
- >>> p
- 2
- >>> myList = [10,20,30,40,50]
- >>> rmovIndxNo = 3
- >>> del myList[rmovIndxNo]
- >>> myList
- [10, 20, 30, 50]
- >>> l = [1, 2, 3, 4, 5]
- >>> l.pop(2)
- 3
- >>> l
- [1, 2, 4, 5]
- >>> l = [1, 2, 3, 4, 5]
- >>> del l[2]
- >>> l
- [1, 2, 4, 5]
- a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- # remove the element at index 3
- a[3:4] = []
- # a is now [0, 1, 2, 4, 5, 6, 7, 8, 9]
- # remove the elements from index 3 to index 6
- a[3:7] = []
- # a is now [0, 1, 2, 7, 8, 9]
- letters = ["a", "b", "c", "d", "e"]
- letters.remove(letters[1])
- print(*letters) # Used with a * to make it unpack you don't have to (Python 3.x or newer)
- del my_list[2]
- del my_list[3]
- del my_list[7]
- new list = [j for i, j in enumerate(my_list) if i not in [2, 3, 7]]
- list = [1, 2, 3, 4]
- list.remove(1)
- print(list)
- output = [2, 3, 4]
- list = [1, 2, 3, 4]
- list.remove(list[2])
- print(list)
- output : [1, 2, 4]
- a = ['a', 'b', 'c', 'd']
- def remove_element(list_,index_):
- clipboard = []
- for i in range(len(list_)):
- if i is not index_:
- clipboard.append(list_[i])
- return clipboard
- print(remove_element(a,2))
- >> ['a', 'b', 'd']
- if index_<0:index_=len(list_)+index_
- >>> test = ['item1', 'item2']
- >>> test.pop(-1)
- 'item2'
- >>> test
- ['item1']
- l = range(20)
- inds2rem = [2,5,1,7]
- map(lambda x: l.pop(x), sorted(inds2rem, key = lambda x:-x))
- >>> l
- [0, 3, 4, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
- del listName[-N]
- del listName[-3:]
- del listName[-8:]
- print([v for i,v in enumerate(your_list) if i not in list_of_unwanted_indexes])
- print([v for i,v in enumerate(your_list) if i != unwanted_index])
- >>> l = [1,2,3,4,5]
- >>> del l[-1:]
- >>> l
- [1, 2, 3, 4]
- >>> l.pop(-1)
- 4
- >>> l
- [1, 2, 3]
- >>> a=[1,2,3,4,5]
- >>> del a[1]
- >>> a
- [1, 3, 4, 5]
- >>> a.pop(1)
- 3
- >>> a
- [1, 4, 5]
- >>>
- >>> l = [0,1,2,3,4,5,6,7,8,9]
- >>> indices=[3,7]
- >>> for i in indices:
- ... del l[i]
- ...
- >>> l
- [0, 1, 2, 4, 5, 6, 7, 9]
- >>> l = [0,1,2,3,4,5,6,7,8,9]
- >>> indices=[3,7]
- >>> for i in sorted(indices, reverse=True):
- ... del l[i]
- ...
- >>> l
- [0, 1, 2, 4, 5, 6, 8, 9]
- v = [1, 2, 3, 4, 5, 6]
- v.remove(v[4]) # I'm removing the number with index 4 of my array
- print(v) # If you want verify the process
- # It gave me this:
- #[1,2,3,4,6]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement