How to Interchange First & Last Elements in a List using Python

In this tutorial, we’ll learn how to program "How to Interchange First & Last Elements in a List using Python." We’ll focus on swapping the first and last elements of a list. The objective is to carefully swap the positions of these elements in the list. A sample program will be provided to demonstrate the coding process, making it simple and easy to understand. So, let’s get started!

This topic is straightforward to understand. Just follow the instructions I provide, and you'll be able to complete it with ease. The program I'll show you demonstrates the proper way to swap the first and last elements in a list. I'll also provide a simple and efficient method to achieve this effectively. So, let’s start 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. def interChange(newList):
  2.     size = len(newList)
  3.    
  4.  
  5.     temp = newList[0]
  6.     newList[0] = newList[size - 1]
  7.     newList[size - 1] = temp
  8.    
  9.     return newList
  10.    
  11.  
  12. while True:
  13.     print("\n================= Interchange First & Last Elements in a List =================\n\n")
  14.     mylist = [34, 21, 14, 54, 43]
  15.  
  16.    
  17.     print("Current List: ", mylist)
  18.  
  19.     print("\nNew List: ", interChange(mylist))
  20.  
  21.     opt = input("\nDo you want to try again?(yes/no): ")
  22.  
  23.     if opt.lower() == 'yes':
  24.         ret=False
  25.     elif opt.lower() == 'no':
  26.         ret=True
  27.         print("Exiting program....")
  28.     else:
  29.         print("Please enter yes/no:")
  30.         break
  31.  
  32.     if ret == False:
  33.         continue

This program swaps the first and last elements of a given list and provides the user with the updated list. Initially, the list [34, 21, 14, 54, 43] is displayed as the current list. The function interChange calculates the size of the list, stores the first element in a temporary variable, and then replaces the first element with the last one. The last element is then replaced by the stored first element from the temporary variable. The modified list is then printed as the new list. Afterward, the program prompts the user to decide whether to repeat the process with the same logic or exit the program.

Output:

There you have it we successfully created How to Interchange First & Last Elements 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