For the past few years, Python has become one of the most popular programming languages in the dev world. It is often used to build websites and software, it is also used to automate tasks and conduct data analysis.
It is also one of the easiest languages to learn, because of its simple syntax. Compared to other languages, Python is very easy to read because its syntax indicates the English language. It is probably one of the best languages to get started with.
In this post, I am going to show you some of the most useful array methods for Python, but first, let's create our first array called
heroes = ["Batman", "Spiderman", "Superman"]
clear() method removes all of the elements in the array. It will leave you with an empty array.
This method adds a new element to the back of your array. We just have to pass in the new element inside the parentheses. We can append strings, numbers, even other arrays. This is a pretty useful command.
Now if we print out our array we should get this output:
['Batman', 'Spiderman', 'Superman', 'Thanos']
As you can notice, it's pretty weird to have
Thanos in the heroes array. Luckily for us, there is a method that will help us take care of the problem which is called:
This method removes the element from the specified index.
If you don't know, arrays start at index
0. So the first item in our array won't start at index
1 but index
0. Don't worry if it's hard to remember at the beginning.
This means that the element we want to remove -
Thanos is at index
3 because we added it to the end of our array. So in order to remove it, we have to write the following command:
After running this command our output should look like this again:
['Batman', 'Spiderman', 'Superman']
But what happens if we don't know at what index the element that we want to remove is. Well, we can use the
index() method. This method returns the index of the first element with the specific value. This method looks something like this:
So if we don't know the index, of the element, and we want to remove it, we can just add the
pop() and the
index() methods together like this:
This method is useful when you want to remove for example an element that has the value of a variable.
But what if I told you there is an easier way to remove a specific item from an array. Well, there is one which is called:
remove() method removes the first item with the specified value. We just have to pass in the specified value inside the parentheses, and it will remove it from the array.
This method may seem easier than the two methods we combined earlier, but it's good to know that there are a couple of ways of doing something.
This method allows us to insert a new element at a specific index. We just have to add the index at which we want to add the element, and then the element itself inside the parenthesis.
As you can see Python is pretty easy to write and use. I hope that this post has helped you with getting more comfortable with writing Python.
If you have any feedback please be sure to leave it in the comments down below, or just message me on Twitter.