3 Methods of Reversing a User Input List in Python

 

    

   # defining empty list
uList = []
# defining the number of elements in the list
nList = int(input("List of how many elements? "))
print("Now enter the elements of your list one by one.")
# taking elements of the list as user input
for i in range(0,nList):
    element = int(input())
    uList.append(element)
    
print(f"The original list is {uList}")


# taking copy of the list, storing it in variable, then reversing it
reverse1 = uList[:] 
reverse1.reverse()
print(f"The first reverse of the original list {uList} is {reverse1}")

reverse2 = uList[::-1]
print(f"The second reverse of the original list {uList} is {reverse2}")

reverse3 = uList[:]
for i in range(len(reverse3)//2):
    reverse3[i], reverse3[len(reverse3)-i-1] = reverse3[len(reverse3)-i-1], reverse3[i]
print(f"The third reverse of the original list {uList} is {reverse3}")

if reverse1 == reverse2 and reverse2 == reverse3:
    print("All the three methods reverse the original list in the same way.")

    

Comments

Popular posts from this blog

Quotation marks to wrap an element in HTML

What is the difference between iostream and iostream.h in cpp?

The Basic Structure of a Full-Stack Web App