Practice Adding and Removing Elements
00:00 In this lesson, we’ll take a look at how you can add and remove elements to lists inside of IDLE. Here you’re going to work with a slightly different example.
00:08 So I’m just going to be working with colors instead of the numbers list. I’ve added color emoji dots, but then also the starting letter of the color so that everyone can understand what these colors are if in case they look the same to you, they’re meant to be different color dots.
00:24
But you can also refer to the letters on the side. So the first one is a red color dot, and we also have an r
next to it. And then there’s a green color dot, and we have a g
next to it, and a blue color dot with a b
next to it.
00:36
Okay, so we have the RGB to start off with, and now let’s mutate this list. colors
looks like this. It contains RG and B, red, green, and blue.
00:48
And now I want to insert something. Let’s insert orange after red. So I’m going to say colors.insert()
.
00:58 And then I need to first give the index position where I want to insert it, and then the object I want to insert. So I want to insert orange at the index one, which is going to be after red and before green.
01:09 And then I will need to create this string, consists of
01:14
orange, the orange color dot, and then I’ll put an o
in here for orange.
01:22
Okay, so I’m calling the .insert()
method on the colors
object, so the list, and now you can see that Python mutated the list, and it added this new color at that specified position.
01:35
So now we have red, orange, green, and blue in the colors list. So another one you’ve heard about before is that you can append an element. So that goes by saying colors.append()
.
01:47 And this is going to add the object at the end of the list. You can see also the pop-ups here in IDLE give you the information. So let’s add some violet here at the end.
01:58
So I’m creating the string that I want to add and then pick out the violet color dot and add a v
for violet. And after executing this, you can see that the colors list now contains red, orange, green, blue, and violet.
02:14
So violet, the string with the emoji and the v
, got added at the end of the list.
02:21 And then you can also extend a list. Let’s start with a different list. For that, I’m going to use a list that contains just the warm tones. So we have red, orange, and yellow in here in that list, and I want to extend it with green, blue, and violet.
02:41
So I need to pass a collection to extend()
.
02:46
Let’s try that first. warm_tones.extend()
.
02:52
Any sort of collection works, so here I’m passing a string, which looks like it’s a single element, but it consists of two characters and it’s iterable. So you can see that warm_tones
changed in a way that I didn’t intend.
03:06
So now I have like 'h'
and 'i'
here at the end, which is not really fitting well with my rainbow idea here. So I’m going to reset warm_tones
and take off this 'h'
and 'i'
.
03:18 So what I wanted to show you here is that if you attempt to actually add just a single element, so let me give you something that is not iterable, like an integer.
03:28 Python’s going to give you a traceback, an error where it says that the integer object is not iterable. So strings are iterable, which means that you can add them like that.
03:37
And you can also use any other sort of iterable. We know that tuples and lists are both iterable. So let’s extend warm_tones
to add green, blue, and violet to it.
03:49 You know those are not warm colors.
03:51
Let’s look at that. warm_tones.extend()
and then pass in an iterable. I’m going to do a tuple. You could also do a list instead. So here I’m adding a tuple that has three values in it, green, blue, and violet.
04:10
And now if you look at warm_tones
, you can see that it contains all of the rainbow colors again: red, orange, yellow, green, blue, and violet.
04:20
It’s not great naming anymore because these are not all warm tones. warm_tones
shouldn’t contain green, blue, and violet actually. So let’s get rid of them. I’m going to say warm_tones.pop()
and remove.
04:34 Let’s take off blue at first. That seems like the least warm for me. So I’m going to figure out at which index position is this? Red is at zero, orange is at one, yellow is at two, green is at three, and blue is at four.
04:48
So when I say warm_tones.pop()
and pass in 4
, the index position of blue, and then run this. And you can see that Python returns the element that it popped out of the list.
05:00
So I get blue as a return value of this operation. And warm_tones
now does not contain blue anymore.
05:08
Let’s also get rid of the other two. So if you don’t define any sort of index position, then Python just pops from the end. So if I run warm_tones.pop()
, then it’ll give me back violet, and it has removed it from warm_tones
.
05:24 And let’s do that same thing again,
05:28
.pop()
without an index position, and then also green has to go.
05:35 And we’re back with the list of actually just the warm color tones: red, orange, and yellow.
05:41 This is just a slightly different example than the numbers list that you’ve seen in the slides. I hope it’s maybe visually a bit more interesting for you to see like how lists are mutable and how you can use these handy list methods to insert elements, append elements at the end, pop elements from somewhere in the list or from the end, and also extend the list with an iterable so you can add more elements at the same time.
06:05 All right, enough with adding and removing elements. In the next lesson, you’ll take a look at what you can do with lists of numbers.
Become a Member to join the conversation.