1myList.remove(item) # Removes first instance of "item" from myList
2myList.pop(i) # Removes and returns item at myList[i]
1# Example 1:
2# animals list
3animals = ['cat', 'dog', 'rabbit', 'guinea pig']
4# 'rabbit' is removed
5animals.remove('rabbit')
6# Updated animals List
7print('Updated animals list: ', animals)
8
9
10# Example 2:
11# animals list
12animals = ['cat', 'dog', 'dog', 'guinea pig', 'dog']
13# 'dog' is removed
14animals.remove('dog')
15# Updated animals list
16print('Updated animals list: ', animals)
17
18# Example 3:
19# animals list
20animals = ['cat', 'dog', 'rabbit', 'guinea pig']
21# Deleting 'fish' element
22animals.remove('fish')
23# Updated animals List
24print('Updated animals list: ', animals)
1l = list[1, 2, 3, 4]
2l.pop(0) #remove item by index
3l.remove(3)#remove item by value
4#also buth of the methods returns the item
1myList = ['Item', 'Item', 'Delete Me!', 'Item']
2
3del myList[2] # myList now equals ['Item', 'Item', 'Item']
1# Basic syntax:
2my_list.remove(element)
3
4# Note, .remove(element) removes the first matching element it finds in
5# the list.
6
7# Example usage:
8animals = ['cat', 'dog', 'rabbit', 'guinea pig', 'rabbit']
9animals.remove('rabbit')
10print(animals)
11--> ['cat', 'dog', 'guinea pig', 'rabbit'] # Note only 1st instance of
12# rabbit was removed from the list.
13
14# Note, if you want to remove all instances of an element, convert the
15# list to a set and back to a list, and then run .remove(element) E.g.:
16animals = list(set['cat', 'dog', 'rabbit', 'guinea pig', 'rabbit']))
17animals.remove('rabbit')
18print(animals)
19--> ['cat', 'dog', 'guinea pig']