How to Update Element in a List using Python

In this tutorial, we will program 'How to Update an Element in a List using Python'. We will learn how to update an element in a list in Python. The main goal here is to create a safe way to store temporary updated data in your current list. I will provide a sample program to demonstrate the actual coding of this tutorial.

This topic is very easy to understand; just follow the instructions I provide, and you can also do it yourself with ease. The program I will show you covers the basics of programming for updating an element in a list. I will do my best to provide you with the easiest method of updating any element in a list. So, let's start with the coding.

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. while True:
  2. ret = False
  4. print("====================== Update Element in a List ====================== ")
  6. myList = ['JavaScript', 'Java', 'PHP'];
  7. print("\nMy current List: ", myList);
  8. print("\n")
  9. sel = int(input("Select an element index to change(1-3): "))
  10. if sel > 3:
  11. print("Error Selection!")
  12. break
  13. else:
  14. el = input("Enter your element: ")
  15. myList[sel-1] = el
  16. print("My new List: ", myList)
  19. opt = input("Do you want to update again?(yes/no): ")
  21. if opt.lower() == 'yes':
  22. ret=False
  23. elif opt.lower() == 'no':
  24. ret=True
  25. print("Exiting program....")
  27. else:
  28. print("Please enter yes/no:")
  29. break
  31. if ret == False:
  32. continue

In the provided code, we simply enclosed the code with a while loop to allow for continuous execution of the program. To update the current list, the trick here is that you need to select the index position first of the said element. Then, set that with the new element you are about to enter from the input.


The How to Update Element in a List using Python source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Update Element in a List using Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials

Add new comment