How to Append an Element to a List in Python

In this tutorial, we will program 'How to Append an Element to a List in Python'. We will learn how to add an element to a list in Python. The main goal here is to create a safe way to store temporary data that can be sent to a working database. 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 appending an element to a list. I will do my best to provide you with the easiest method of appending any element to 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. my_list = []
  2.  
  3. print("\n\n=============== Append an Element to a List ===============\n\n")
  4.  
  5. while True:
  6. ret=False
  7.  
  8.  
  9. my_input = input("Enter a string: ")
  10.  
  11.  
  12. my_list.append(my_input)
  13.  
  14. print(my_list)
  15.  
  16.  
  17. opt = input("\nDo you want to append another string? (Yes/No): ");
  18. print("\n")
  19.  
  20. if opt.lower() == "yes":
  21. ret=True
  22.  
  23. elif opt.lower() == "no":
  24. ret=False
  25.  
  26. print("Exiting Program...")
  27. else:
  28. print("Please enter Yes or No\n")
  29. break
  30.  
  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. This code is very simple and easy to do; you just append the inputs into an array list using the append function.

Output:

The How to Append an Element to a List in 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 Append an Element to a List in 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

Tags

Add new comment