Python Program to Swap the First and Last Value of a List

Lists in Python:

A list is exactly what it sounds like: a container for various Python objects such as integers, words, values, and so on. In other programming languages, it is equal to an array. It is denoted with square brackets (and this is one of the attributes that differentiates it from tuples, which are separated by parentheses). It is also mutable, which means it can be changed or altered, as opposed to tuples, which are immutable.

Given a list , the task is to swap the first and last value of the given list.

Examples:

String List:

Example1:

Input:

given list = ['hello', 'this', 'is', 'BTechGeeks', 'online', 'platform', 'for', 'btech', 'students']

Output:

printing the given list before swapping the values = 
['hello', 'this', 'is', 'BTechGeeks', 'online', 'platform', 'for', 'btech', 'students']
printing the given list after swapping the values = 
['students', 'this', 'is', 'BTechGeeks', 'online', 'platform', 'for', 'btech', 'hello']

Integer List:

Example2:

Input:

given list = [33, 92, 12, 74, 22, 79, 122, 17, 63, 99, 129]

Output:

printing the given list before swapping the values = 
[33, 92, 12, 74, 22, 79, 122, 17, 63, 99, 129]
printing the given list after swapping the values = 
[129, 92, 12, 74, 22, 79, 122, 17, 63, 99, 33]

Program to Swap the First and Last Value of the Given List in Python

There are several ways to sort the first and last value of the given list in python some of them are:

Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.

Method #1:Using temporary variable

Approach:

  • Give the live input as static.
  • Swap the first and last elements in the given list using a temporary variable.
  • Print the new list after swapping that.
  • The Exit of the Program

i)String list

Below is the implementation:

# given list
given_list = ['hello', 'this', 'is', 'BTechGeeks',
              'online', 'platform', 'for', 'btech', 'students']
# printing the given list before swapping the values
print('printing the given list before swapping the values = ')
print(given_list)
# Swap the first and last elements in the given list using a temporary variable.
tempo = given_list[0]
given_list[0] = given_list[-1]
given_list[-1] = tempo
# printing the given list after swapping the values
print('printing the given list after swapping the values = ')
print(given_list)

Output:

printing the given list before swapping the values = 
['hello', 'this', 'is', 'BTechGeeks', 'online', 'platform', 'for', 'btech', 'students']
printing the given list after swapping the values = 
['students', 'this', 'is', 'BTechGeeks', 'online', 'platform', 'for', 'btech', 'hello']

ii)Integer list

Below is the implementation:

# given list
given_list = [33, 92, 12, 74, 22, 79, 122, 17, 63, 99, 129]
# printing the given list before swapping the values
print('printing the given list before swapping the values = ')
print(given_list)
# Swap the first and last elements in the given list using a temporary variable.
tempo = given_list[0]
given_list[0] = given_list[-1]
given_list[-1] = tempo
# printing the given list after swapping the values
print('printing the given list after swapping the values = ')
print(given_list)

Output:

printing the given list before swapping the values = 
[33, 92, 12, 74, 22, 79, 122, 17, 63, 99, 129]
printing the given list after swapping the values = 
[129, 92, 12, 74, 22, 79, 122, 17, 63, 99, 33]

Method #2: Using comma operator in Python

Approach:

  • Give the live input as static.
  • Swap the first and last elements in the given list using a ‘,’ operator.
  • Print the new list after swapping that.
  • The Exit of the Program

i)String list

Below is the implementation:

# given list
given_list = ['hello', 'this', 'is', 'BTechGeeks',
              'online', 'platform', 'for', 'btech', 'students']
# printing the given list before swapping the values
print('printing the given list before swapping the values = ')
print(given_list)
# Swap the first and last elements in the given list using a , operator
given_list[0], given_list[-1] = given_list[-1], given_list[0]
# printing the given list after swapping the values
print('printing the given list after swapping the values = ')
print(given_list)

Output:

printing the given list before swapping the values = 
['hello', 'this', 'is', 'BTechGeeks', 'online', 'platform', 'for', 'btech', 'students']
printing the given list after swapping the values = 
['students', 'this', 'is', 'BTechGeeks', 'online', 'platform', 'for', 'btech', 'hello']

ii)Integer list

Below is the implementation:

# given list
given_list = [33, 92, 12, 74, 22, 79, 122, 17, 63, 99, 129]
# printing the given list before swapping the values
print('printing the given list before swapping the values = ')
print(given_list)
# Swap the first and last elements in the given list using a , operator
given_list[0], given_list[-1] = given_list[-1], given_list[0]
# printing the given list after swapping the values
print('printing the given list after swapping the values = ')
print(given_list)

Output:

printing the given list before swapping the values = 
[33, 92, 12, 74, 22, 79, 122, 17, 63, 99, 129]
printing the given list after swapping the values = 
[129, 92, 12, 74, 22, 79, 122, 17, 63, 99, 33]

Related Programs: