MODIFICATION WITH PYTHON LIST: UPDATION, INSERTION AND DELETION

in Steemit Nursery3 years ago


python list.png

Hello everyone, this is my third post in this Steemit community. While navigating across Steemit, I came across this "Steemit Nursery" that is for newcomers in Steemit. So, I decided to post my tutorial in the same community. The last post of mine discuss the theoretical concepts of list along with accessing the elements of it and using various python in-built function that you can apply to the list. In the part, I am going to see how you can modify your list. Modification of list involves mainly three important concepts: updating the value of items in the list, inserting new elements and removing the items from the list. I am gonna show you how to do all of these with every possible alternatives that the python provides. I am going to use Sublime Text for this and you can use any IDE of your choice like PyCharm, Visual Studio, IDLE, PyDev, Jupyter Notebook and so on.

I will start with the same list of countries that we did in the previous post. First lets see how we can update the value of any items in the list. Updating is done by accessing the index number of items and then assigning a new value to it. For example consider the below code:

countries = ["USA", "Russia", "Germany", "Korea", "Spain", "South Africa", "Pakistan", "Israel"]

print("The original list is: ", countries)

countries[3] = "Thailand"

print()

print("The list after updation is:", countries)

Here we want to change "Korea" to "Thailand". So Korea has index value of 3 and then we assign the value "Thailand" to that index number in the 3rd line. Now the list holds value Thailand in the fourth position i.e. index number 3. If you run this program, you will get the following output.

image.png

Now lets dive into how we can add new elements to a list. It can be done in two ways. First way is by using in-built append() function. Using this function will add new items to the end of the list. Suppose we want to add "Thailand" to the above original list. We will do this by writing following code:

countries = ["USA", "Russia", "Germany", "Korea", "Spain", "South Africa", "Pakistan", "Israel"]

print("The original list is: ", countries)

countries.append("Thailand")

print()

print("The list after addition is:", countries)

Lets see what we will get in the execution screen:

image.png

Another way to add new items into the list is with the help of insert() function. While append() will add items to the end of the list, insert() will add items into the position that you specify in it. For example if we want to insert Thailand in fourth position in the above original list, we will write as countries.insert(3, "Thailand"). Remeber, index starts at 0 for first position in the list and this continues.

countries = ["USA", "Russia", "Germany", "Korea", "Spain", "South Africa", "Pakistan", "Israel"]

print("The original list is: ", countries)

countries.insert(3, "Thailand")

print()

print("The list after addition is:", countries)

Now you can see, Thailand being added to the fourth position in the list as below:

image.png

Now the las thing is about deleting the items from the list. You can delete an item in three ways. The first way is using del statement. It is very simple to use, you just need to specify the index number of element you want to delete. For example, lets consider this code:

countries = ["USA", "Russia", "Germany", "Korea", "Spain", "South Africa", "Pakistan", "Israel"]

print("The original list is: ", countries)

del countries[5]

print()

print("The list after deletion is:", countries)

In the above code, we have specified that we wanted to delete the sixth positioned element i.e. South Africa which is index number 5 and you can see it in the output below.

image.png

Another way to delete the item is using remove() statement. It is beneficial to use in the scenario when you have thousands of elements in the list and you don't know which index number to delete but you know the value. So this is useful when you know the value. In above example, instead of the position, we can specify the value- South Africa as:


countries = ["USA", "Russia", "Germany", "Korea", "Spain", "South Africa", "Pakistan", "Israel"]

print("The original list is: ", countries)

countries.remove("South Africa")

print()

print("The list after deletion is:", countries)

Upon running the program, you can see the following output:

image.png

The third way to delete an item from the list is with the help of pop() function. This function is a common usage in one of the important data structure i.e. Stack. It will delete the last item from the list. What makes it different from other methods is that even after removing the item from the list, it helps you to work with the original list. Lets see a simple code for this one:

countries = ["USA", "Russia", "Germany", "Korea", "Spain", "South Africa", "Pakistan", "Israel"]

print("The original list is: ", countries)

a = countries.pop()

print()

print("The removed country is:", a)

print()

print("The list after deletion is:", countries)

We have stored the name of country that has been popped from the list in a variable named as "a". It will remove Israel from the list of country as below:

image.png

Another cool feature of this method is that you can also pass index number of an element to the method that you want to remove. Lets say we want to pop Germany i.e. index number 2. We can write the code as below:

countries = ["USA", "Russia", "Germany", "Korea", "Spain", "South Africa", "Pakistan", "Israel"]

print("The original list is: ", countries)

a = countries.pop(2)

print()

print("The removed country is:", a)

print()

print("The list after deletion is:", countries)

You can now see Germany being removed from our list.

image.png

Ok then this concludes the end of our post. If you have any trouble grasping the concept or running this code, then please drop a comment below. I would be happy to help you. Hope to see you in my next post. Till then stay safe and STEEM on!

Sort:  
 3 years ago 

Hello @coder-geek,

Welcome to the Steemit Nursery family! We are pleased to onboard you as a #Newborn in our community.

This is a community for you, like Steemit Newborn, to be accompanied at the beginning of your trip on Steemit.

Please note that Steemit is blockchain-based blogging and social networking site that rewards users in STEEM cryptocurrency for posting and curating material. Do participate by making posts, commenting, and voting on others' posts.

Best regards,
Steemit Nursery Team

Link:
Steemit Nursery Community Announcement updated on 25/05/2021

 3 years ago 

Thank you. I will follow the guidelines stated by you🙂.

 3 years ago 

Good post

 3 years ago 

Thanks mate🙂

 3 years ago 

Thanks you too mate

Coin Marketplace

STEEM 0.20
TRX 0.14
JST 0.030
BTC 67364.26
ETH 3322.90
USDT 1.00
SBD 2.71