Posted By

denisemauldin on 06/03/16


python array print reverse

Versions (?)

Who likes this?

2 people have marked this snippet as a favorite


Python Reverse Array

 / Published in: Python

arr[::-1] does a list slice to reverse the array

str(x) for x in arr[::-1] casts the ints in the array as strings

map(str, arr[::-1]) does the same thing by using map to cast the ints

" ".join() joins the resulting strings with a space

  1. #!/bin/python3
  3. import sys
  5. n = int(input().strip())
  6. arr = [int(arr_temp) for arr_temp in input().strip().split(' ')]
  7. print(" ".join(str(x) for x in arr[::-1]))
  8. print(" ".join(map(str, arr[::-1]))) # or
  9. print(' '.join(map(str, reversed(arr)))) # or

Report this snippet  

You need to login to post a comment.