Gigili
0
Q:

remove element from list python

myList.remove(item) # Removes first instance of "item" from myList
myList.pop(i) # Removes and returns item at myList[i]
45
myList = ['Item', 'Item', 'Delete Me!', 'Item']

del myList[2] # myList now equals ['Item', 'Item', 'Item']
6
list.remove(element)
11
>>> a = [0, 2, 3, 2]
>>> a.remove(2)
>>> a
[0, 3, 2]
10
# delete by index
a = ['a', 'b', 'c', 'd']
a.pop(0)
print(a)
['b', 'c', 'd']
6
>>> a = ['a', 'b', 'c', 'd']
>>> a.remove('b')
>>> print a
['a', 'c', 'd']
7
# Basic syntax:
my_list.remove(element)

# Note, .remove(element) removes the first matching element it finds in
# 	the list.

# Example usage:
animals = ['cat', 'dog', 'rabbit', 'guinea pig', 'rabbit']
animals.remove('rabbit')
print(animals)
--> ['cat', 'dog', 'guinea pig', 'rabbit'] # Note only 1st instance of
#	rabbit was removed from the list. 

# Note, if you want to remove all instances of an element, convert the
#	list to a set and back to a list, and then run .remove(element)	E.g.:
animals = list(set['cat', 'dog', 'rabbit', 'guinea pig', 'rabbit']))
animals.remove('rabbit')
print(animals)
--> ['cat', 'dog', 'guinea pig']
3
a = [10, 20, 30, 20]
a.remove(20)
# a = [10, 30, 20]
# removed first instance of argument
8
list.remove(item)
10
# animals list
animals = ['cat', 'dog', 'rabbit', 'guinea pig']

# 'rabbit' is removed
animals.remove('rabbit')

# Updated animals List
print('Updated animals list: ', animals)
3

New to Communities?

Join the community