1
00:00:04,710 --> 00:00:09,750
Alright, so let's talk about removing items
from a list. So we're going to modify our

2
00:00:09,750 --> 00:00:14,190
program to allow the customer to change
their mind, and remove items from their

3
00:00:14,190 --> 00:00:18,820
list. Our program's a bit limited
because we're not using a GUI interface.

4
00:00:18,820 --> 00:00:22,770
Most likely our customer would be using
their browser to purchase parts, and

5
00:00:22,770 --> 00:00:26,520
would have a drop-down to indicate how
many of each item they want. So bear in

6
00:00:26,520 --> 00:00:29,820
mind that we're learning how to
manipulate lists here - we're not learning

7
00:00:29,820 --> 00:00:33,570
how to write an online shopping cart.
Having said that, though, the code for an

8
00:00:33,570 --> 00:00:37,680
online cart would contain all the
techniques that we're using here. So the

9
00:00:37,680 --> 00:00:42,460
code we need to change, is this code on
lines 18 through 21.

10
00:00:42,460 --> 00:00:46,820
When we get to that if block, we know
which part the customer's chosen. At the

11
00:00:46,829 --> 00:00:51,360
moment we add that part. What we want to
do, is check first to see if the part's

12
00:00:51,360 --> 00:00:56,430
already in their list. Let's have a go at
doing that. So basically, under line 20

13
00:00:56,430 --> 00:01:03,120
here, we're going to type if chosen
underscore part is in computer underscore

14
00:01:03,120 --> 00:01:07,590
parts : I'm just going to put a
comment there for the moment to say, it's

15
00:01:07,590 --> 00:01:15,510
already in, so remove it, and we'll leave
line 23 as is. Now ignore the error on

16
00:01:15,510 --> 00:01:19,409
line 22 at the moment - we saw that before
in an earlier video. If you want to you

17
00:01:19,409 --> 00:01:22,770
could add a pass statement to fix that
error - I'll just add a pass statement for

18
00:01:22,770 --> 00:01:29,220
now. All right, so how do we actually
remove an item from a list. Well let's go

19
00:01:29,220 --> 00:01:34,140
back to that table that we looked at
previously. If we scroll down and have a

20
00:01:34,140 --> 00:01:39,210
look here, you can see we've got a remove
method. We tell it which item to remove

21
00:01:39,210 --> 00:01:43,590
and it removes it from the list. Now
it's always worth reading any notes when

22
00:01:43,590 --> 00:01:47,549
you're checking how to use functions and
methods. The remove method has Note 3

23
00:01:47,549 --> 00:01:53,130
in the last column, and you can see there
under notes: raises ValueError when X is

24
00:01:53,130 --> 00:01:57,630
not found in s. So in other words, we'll
get an error if the item isn't in the

25
00:01:57,630 --> 00:02:01,259
list. So that's something just to be
aware of. Later in the course, we'll see

26
00:02:01,260 --> 00:02:05,180
how to handle errors like that. In this
case we don't have to worry about it,

27
00:02:05,180 --> 00:02:08,280
and that's because - and I'll switch back to
the code here -

28
00:02:08,288 --> 00:02:12,909
on line 21, we've already checked to see
if the part is in the list. So that means

29
00:02:12,909 --> 00:02:17,290
we can safely remove it without worrying
about getting an error. Alright, so

30
00:02:17,290 --> 00:02:21,489
let's replace this pass statement with
the actual code, which will be computer

31
00:02:21,489 --> 00:02:27,340
underscore parts dot remove, and in
parentheses, chosen underscore part. Okay,

32
00:02:27,340 --> 00:02:31,510
so if the chosen part isn't in the list
then we want to add that, and we can do

33
00:02:31,510 --> 00:02:35,620
that by putting line 24 inside an else
block. So we're gonna go ahead and do

34
00:02:35,620 --> 00:02:42,300
that - else : then on the next line, come
back over here and indent that correctly,

35
00:02:42,300 --> 00:02:45,910
and that's all we need to do. It would be
nice, though, to remind our customer of

36
00:02:45,910 --> 00:02:49,690
what's in their list, though, so I'm going
to add a line and print the list out, and

37
00:02:49,690 --> 00:02:55,329
I'll do that outside of that block. So
I'm going to type print, in parentheses,

38
00:02:55,329 --> 00:03:06,080
double quotes Your list now contains :
left and right curly braces dot format,

39
00:03:06,080 --> 00:03:09,790
and in parentheses, computer
underscore parts, and the two closing

40
00:03:09,790 --> 00:03:14,829
right parentheses there. Now be careful
over the indentation for line 26. We want

41
00:03:14,829 --> 00:03:18,340
to print it out whether the customer
added or removed an item, which means it

42
00:03:18,340 --> 00:03:21,340
mustn't be indented inside the else
block, and you can see in my case that

43
00:03:21,340 --> 00:03:26,020
it's not. Alright, so I'm gonna run the
program now and see how it behaves.

44
00:03:26,020 --> 00:03:33,240
I'll add option one first, a computer, then
option two for a monitor. You can see

45
00:03:33,250 --> 00:03:36,970
that each time I'm doing that, we get the new
item list printed out. Now it turns out

46
00:03:36,970 --> 00:03:39,459
that I've got a spare monitor kicking
about, so I'm going to use option two

47
00:03:39,459 --> 00:03:43,569
again now, to remove the monitor from the
list. The list now only contains

48
00:03:43,569 --> 00:03:47,230
computer, so that's working well. It did
say it was adding to when it really

49
00:03:47,230 --> 00:03:52,180
meant removing it, though. But we can
tidy that up by cutting line 18 out, and

50
00:03:52,180 --> 00:03:59,190
I'll exit out of the program first. So what
we want to do there, is cut out line 18,

51
00:03:59,190 --> 00:04:06,069
entirely, and we want to put it inside
the else statements are down here,

52
00:04:06,069 --> 00:04:10,180
because this is the code that's executed
when we add an item. But let's also take

53
00:04:10,180 --> 00:04:16,180
the opportunity here as well, paste it in
here as well, and instead of Adding,

54
00:04:16,180 --> 00:04:19,459
we're gonna change that to Removing.

55
00:04:21,500 --> 00:04:26,550
Alright so let's run the program again - run it
exactly the same as we did before: one,

56
00:04:26,550 --> 00:04:32,100
computer, two, monitor and you get Adding each
time. We'll add, this time we'll remove

57
00:04:32,100 --> 00:04:37,919
the monitor again, two, says Removing
two, our list now only contains computer,

58
00:04:37,919 --> 00:04:42,449
and we'll exit out of it. Alright.
So that's how you can append items to a

59
00:04:42,449 --> 00:04:47,010
list and remove items from it. The append
method is used to add new items. If you

60
00:04:47,010 --> 00:04:50,910
add the same item again then the list
will contain it twice. Lists don't check that

61
00:04:50,910 --> 00:04:55,500
the item that they contain are unique.
The remove method removes items from a

62
00:04:55,500 --> 00:04:59,550
list, and if the item you want to remove
doesn't exist, you'll get an error. So I'm

63
00:04:59,550 --> 00:05:03,150
going to leave the other operations that
you can perform on mutable sequences for

64
00:05:03,150 --> 00:05:07,540
now. In the next video, we'll turn our
attention on how to sort a list.

65
00:05:07,540 --> 00:05:10,660
See you in the next video.

