First and last element of list in python assignment expert

First and Last Elements of List

This Program name is First and Last Elements of List. Write a Python program to First and Last Elements of List, it has two test cases

The below link contains First and Last Elements of List question, explanation and test cases

https://docs.google.com/document/d/1LSR6d1QTXfpHGouv_GmIKkL595o6R_IK/edit?usp=sharing&ouid=104486799211107564921&rtpof=true&sd=true

We need exact output when the code was run

Last half of List:

You are given an integer N as input. Write a program to read N inputs and print a list containing the elements in the last half of the list.

The first line of input is an integer N. The second line contains N space-separated integers.

Explanation

In the example, we are given

6 numbers 1, 2, 3, 4, 5, 6 as input.

The last half of elements of the list are 4, 5, 6. So, the output should be [4, 5, 6].

In the example, we are given

5 numbers 1, 11, 13, 21, 19 as input. The last half of elements of the list are 21, 19. So, the output should be [21, 19].

Sample Input 1

6

1 2 3 4 5 6

Sample Output 1

[4, 5, 6]

Sample Input 2

5

1 11 13 21 19

Sample Output 2

[21, 19]

Home » Python » Python programs

Here, we will write a Python program in which we will interchange the first and list element of the list.
Submitted by Shivang Yadav, on April 06, 2021

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

List is a sequence data type. It is mutable as its values in the list can be modified. It is a collection of ordered sets of values enclosed in square brackets [].

Python program to interchange first and last element in last

In this program, we will swap the first and last elements of the list.

Example:

Input:
[4, 1, 7, 3, 90, 23, 56]

Output:
[56, 1, 7, 3, 90, 23, 4]

Solution Approach:

We will simply need to swap elements at the first index with the element at the last index in the list.

Python provides multiple ways to perform the task. Let's see them,

Method 1:

In this method, we will find the length of the list. And then swap the element at index 0 and element at index (length - 1).

Algorithm:

  • Find the length of the list
  • Swap values at list[0] and list[len - 1]
    • temp = list[0]
    • list[0] = list[length - 1]
    • list[length - 1] = temp
  • Return the list

Program to interchange first and last element in the list

# Python program to interchange the 
# first and last element of a list

# Creating a list 
myList = [1, 7, 3, 90, 23, 4]
print("Initial List : ", myList)

# finding the length of list
length = len(myList)

# Swapping first and last element
temp = myList[0]
myList[0] = myList[length - 1]
myList[length - 1] = temp

print("List after Swapping : ", myList)

Output:

Initial List :  [1, 7, 3, 90, 23, 4]
List after Swapping :  [4, 7, 3, 90, 23, 1]

Method 2:

Another method to interchange values is using Python's shorthand technique to find the last element. And then swapping them. 

The last element can be found using list[-1].

Algorithm:

  • swap -> list[0] and list[-1].
    • temp = list[-1]
    • list[-1] = list[-0]
    • list[0] = temp
  • Print list

Program to interchange first and last element in the list

# Python program to interchange the 
# first and last element of a list

# Creating a list 
myList = [1, 7, 3, 90, 23, 4]
print("Initial List : ", myList)

# Swapping first and last element
temp = myList[-1]
myList[-1] = myList[0]
myList[0] = temp

print("List after Swapping : ", myList)

Output:

Initial List :  [1, 7, 3, 90, 23, 4]
List after Swapping :  [4, 7, 3, 90, 23, 1]

Method 3:

In this method, we will use the comma assignment method from python. Here, we will store the first and last element tuple to the last and first element tuple.

Syntax:

list[0], list[-1] = list[-1], list[0]

Program to interchange first and last element in a list

# Python program to interchange the 
# first and last element of a list

# Creating a list 
myList = [1, 7, 3, 90, 23, 4]
print("Initial List : ", myList)

# Swapping first and last element
myList[0], myList[-1] = myList[-1], myList[0]

print("List after Swapping : ", myList)

Output:

Initial List :  [1, 7, 3, 90, 23, 4]
List after Swapping :  [4, 7, 3, 90, 23, 1]

Method 4:

One more method, can be deleting the elements from the list and then inserting them back in the desired position.

Here, we will delete (pop) the first and the last elements of the list and insert them back as the first element at the last position and last element at the first position.

Algorithm:

  • Swap values
    • firstVal = list.pop(0)
    • lastVal = list.pop(-1)
    • list.insert(0, lastVal)
    • list.append(firstVal)
  • Print list

Program to interchange first and last element in a list

# Python program to interchange the 
# first and last element of a list

# Creating a list 
myList = [1, 7, 3, 90, 23, 4]
print("Initial List : ", myList)

# Swapping first and last element
firstVal = myList.pop(0)   
lastVal = myList.pop(-1)
myList.insert(0, lastVal)  
myList.append(firstVal)

print("List after Swapping : ", myList)

Output:

Initial List :  [1, 7, 3, 90, 23, 4]
List after Swapping :  [4, 7, 3, 90, 23, 1]

Python List Programs »