Last Half of List in Python

Problem Statement:

In the Last Half of List in Python, we need to take a number as input and then a list of numbers. Then, we need to print a list containing half the numbers from the last part of the original list. Now, two cases arise, because the list can be even or odd. If the list is even, it’s simple to access half elements from the last, if the list is odd, make it even by minus 1 from it and proceed further.

Code for Last Half of List in Python:

# taking number  as input
n = int(input("Enter number of elements : "))

# taking n number of numbers as input in a list in single line
elements = list(map(int,input("Enter the numbers : ").strip().split()))[:n]

# declaring an empty list
half_numbers = []

# initializing for loop
for i in range((n//2)+1, n):
    half_numbers.append((elements[i]))

# printing result
print(half_numbers)

Output:

Last Half of List in Python

Also Read:

Share:

Author: Harry

Hello friends, thanks for visiting my website. I am a Python programmer. I, with some other members, write blogs on this website based on Python and Programming. We are still in the growing phase that's why the website design is not so good and there are many other things that need to be corrected in this website but I hope all these things will happen someday. But, till then we will not stop ourselves from uploading more amazing articles. If you want to join us or have any queries, you can mail me at admin@copyassignment.com Thank you