Shahbaz
0
Q:

python remove

myList.remove(item) # Removes first instance of "item" from myList
myList.pop(i) # Removes and returns item at myList[i]
45
list.remove(element)
11
# delete by index
a = ['a', 'b', 'c', 'd']
a.pop(0)
print(a)
['b', 'c', 'd']
6
a = [10, 20, 30, 20]
a.remove(20)
# a = [10, 30, 20]
# removed first instance of argument
8
list.remove(item)
10
# deletes whole list
thislist = ["apple", "banana", "cherry"]
del thislist
4
l = list[1, 2, 3, 4]

for i in range(len(list)):
  l.pop(i) # OR "l.remove(i)"
10
# the list.remove(object) method takes one argument 
# the object or element value you want to remove from the list
# it removes the first coccurence from the list

generic_list = [1, 2, 2, 2, 3, 3, 4, 5, 5, 5, 6, 8, 8, 8]

generic_list.remove(1)

# The Generic list will now be:
# [2, 2, 2, 3, 3, 4, 5, 5, 5, 6, 8, 8, 8]

5
>>> s="abc \n \t \t\t \t \nefg"
>>> ''.join(s.split())
'abcefg'
>>> ''.join(c for c in s if not c.isspace())
'abcefg'

import re

s = 'abc \n \t \t\t \t \nefg'
re.sub(r'\s', '', s)
=> 'abcefg'
0

New to Communities?

Join the community