• We just launched and are currently in beta. Join us as we build and grow the community.

How to Update Element in a List using Python

5oh2modz

Achievement Unlocked
5 Rep
0
0
0
Rep
0
5 Vouches
0
0
0
Vouches
0
Posts
92
Likes
48
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 100 XP
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 https://www.python.org/downloads/.

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

  3. print

    (

    "====================== Update Element in a List ====================== "

    )

  4. myList =

    [

    'JavaScript'

    ,

    'Java'

    ,

    'PHP'

    ]

    ;
  5. print

    (

    "\n

    My current List: "

    ,

    myList)

    ;
  6. print

    (

    "\n

    "

    )
  7. sel =

    int

    (

    input

    (

    "Select an element index to change(1-3): "

    )

    )
  8. if

    sel >

    3

    :
  9. print

    (

    "Error Selection!"

    )
  10. break
  11. else

    :
  12. el =

    input

    (

    "Enter your element: "

    )
  13. myList[

    sel-1

    ]

    =

    el
  14. print

    (

    "My new List: "

    ,

    myList)


  15. opt =

    input

    (

    "Do you want to update again?(yes/no): "

    )

  16. if

    opt.lower

    (

    )

    ==

    'yes'

    :
  17. ret=

    False
  18. elif

    opt.lower

    (

    )

    ==

    'no'

    :
  19. ret=

    True
  20. print

    (

    "Exiting program...."

    )

  21. else

    :
  22. print

    (

    "Please enter yes/no:"

    )
  23. break

  24. if

    ret ==

    False

    :
  25. 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.

Output:

how-to-update-element-in-a-list-using-python-1.png

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


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

335,120

335,128

Top