Syed Abdul Rehman
Syed Abdul Rehman

Reputation: 43

How to reverse a given array of numbers in Python 3?

For example:

I have an array number like

n = int(input().strip())           # 4
arr = map(int,input().strip().split(' ')) #2 4 3 1
print(arr[::-1])

inputs:

4 
2 4 3 1 

My output is [1,3,4,2]

But actual output must be 1 3 4 2

How do I implement this using python 3?

Upvotes: 1

Views: 1579

Answers (2)

Vlad Bezden
Vlad Bezden

Reputation: 89675

you can just unpack list using * operator

>>> arr = [*range(5)]
>>> print(*arr)
0 1 2 3 4

Upvotes: 0

Mureinik
Mureinik

Reputation: 311843

You could re-join the list to a string:

print(" ".join([str(x) for x in arr]))

Upvotes: 1

Related Questions