How to Merge Two List in Python

In this tutorial, we will cover How to Merge Two Lists in Python. The main goal is to understand the concept of merging the elements of the two lists. We 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 is how to combine two lists. I will do my best to provide you with the easiest method of creating this program, called "Merging Two Lists". 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 of merging two list program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. while True:
  2. ret=False
  3.  
  4. print("\n\n================= Merge Two List =================\n\n")
  5. list1 = [1, 2, 3, 4]
  6. list2 = ["a", "b", "c", "d"]
  7.  
  8. mergeList = list1 + list2
  9. print(mergeList)
  10.  
  11.  
  12. user_input=input("\n\nTry again? (Yes/No): ")
  13. print("\n\n")
  14. if user_input.lower()=='no':
  15. print("Exit program.")
  16. break
  17.  
  18. elif user_input.lower()=='yes':
  19. ret=True
  20.  
  21. else:
  22. print("Wrong input.")
  23. ret=True
  24.  
  25.  
  26. if ret:
  27. continued

In the provided code, we use a while loop to enclose the code to ensure the continuation of the program. The code is very straightforward; in order to merge the two lists, we simply concatenate the two lists with the plus symbol.

Output:

The How to Merge Two 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 Merge Two 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