write a python program to swap first and last element of the list

Python programming provides powerful tools for manipulating lists, a versatile data structure used to store collections of items. 

One common task is swapping the first and last elements of a list. In this blog post, I will guide you through the process of writing a Python program to accomplish this task. 

We will start by asking the user to enter the length of the list, then accept each list element using a for loop. 

Next, we will demonstrate how to swap the first and last elements of the list. So let's dive in and explore the code!


write a python program to swap first and last element of the list

 

 

Title : write a python program to swap first and last element of the list

 

 

Accepting List Elements with User Input

To begin, we will prompt the user to enter the length of the list and accept each list element using a for loop. By doing so, we can ensure that the list accurately represents the user's desired input.

    

length = int(input("Enter the length of the list: "))
user_list = []

for i in range(length):
element = input(f"Enter element {i + 1}: ")
user_list.append(element)


In the code above, we use the int() function to convert the user's input for the length into an integer. We then initialize an empty list called user_list. Using a for loop, we iterate from 0 to length - 1, prompting the user to enter each list element and appending it to user_list.

 

Swapping the First and Last Elements

Once we have the user's list, we can proceed to swap the first and last elements. To accomplish this, we will utilize Python's index-based access and leverage the temporary variable concept.


if length > 1:
user_list[0], user_list[-1] = user_list[-1], user_list[0]
print("Swapped List:", user_list)

In the code snippet above, we first check if the length of the list is greater than 1. If it is, we proceed with the swap operation. We take two list indexes 0 and -1 to indicate first and last elements, use python simplest method to swap values present in 0 and -1 positions.

 

Displaying the Result and Handling Edge Cases

Now that we have successfully swapped the first and last elements, we can display the updated list. Additionally, we need to handle scenarios where the list length is less than or equal to 1, as there is no need to perform the swap operation.



else:
print("The list contains only one element or is empty. No swap performed.")


In the code snippet above, we check if the length of the list is greater than 1. If it is, we print the swapped list. Otherwise, we display a message indicating that the list either contains only one element or is empty, and thus no swap is performed.

 

 

Complete Python Program



length = int(input("Enter the length of the list: "))
user_list = []

for i in range(length):
element = input(f"Enter element {i + 1}: ")
user_list.append(element)

if length > 1:
user_list[0], user_list[-1] = user_list[-1], user_list[0]
print("Swapped List:", user_list)
else:
print("The list contains only one element or is empty. No swap performed.")



 

 

Output 1 :

    
Enter the length of the list: 5
Enter element 1: 1
Enter element 2: 2
Enter element 3: 3
Enter element 4: 4
Enter element 5: 5
Swapped List: ['5', '2', '3', '4', '1']


 

Output 2 :

    
Enter the length of the list: 1
Enter element 1: 9
The list contains only one element or is empty. No swap performed.

 

Conclusion:

In this blog post, we have explored the process of writing a Python program to swap the first and last elements of a list. We began by asking the user to enter the length of the list and accepting each list element using a for loop. 

Then, we demonstrated how to swap the first and last elements using index-based access.

Finally, we displayed the result while handling edge cases. By following the steps outlined in this guide, you can confidently manipulate lists and perform similar operations in Python.

 

Post a Comment

0 Comments