-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathremove_from_list.py
48 lines (32 loc) · 918 Bytes
/
remove_from_list.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Python: del vs pop vs remove from a list
# (c) Joe James 2023
def get_dogs():
return ['Fido', 'Rover', 'Spot', 'Duke', 'Chip', 'Spot']
dogs = get_dogs()
print(dogs)
# Use pop() to remove last item or an item by index and get the returned value.
print('1. pop last item from list:')
myDog = dogs.pop()
print(myDog, dogs)
dogs = get_dogs()
print('2. pop item with index 1:')
myDog = dogs.pop(1)
print(myDog, dogs)
# Use remove() to delete an item by value. (raises ValueError if value not found)
dogs = get_dogs()
print('3. remove first Spot from list:')
dogs.remove('Spot')
print(dogs)
# Use del to remove an item or range of items by index. Or delete entire list.
dogs = get_dogs()
print('4. del item with index 3:')
del(dogs[3])
print(dogs)
dogs = get_dogs()
print('5. del items [1:3] from list:')
del(dogs[1:3])
print(dogs)
dogs = get_dogs()
print('6. del entire list:')
del(dogs)
print(dogs)