0

I am attempting to print a reversed array (list) of integers as a single line of space-separated numbers.

I've tried to use a for loop and printed each integer separately with a sep = ", " to remove the commas from the list. Should I use the remove() method as one alternative?

       n = int(input())
       arr = list(map(int, input().rstrip().split()))

       for i in arr[::-1]:
         print(i, sep = ", ")

Output

2
3
4
1

Should be:

2 3 4 1

Suppose I inputted 4. The expected results should be the exact sequence in the output but in a single line.

MT_dev
  • 163
  • 1
  • 10

2 Answers2

3

IIUC, this is what you look for:

print(*reversed(arr))

If a comma separator is needed, you can write

print(*reversed(arr), sep=", ")
GZ0
  • 4,055
  • 1
  • 10
  • 21
1
   for i in arr[::-1]:
     print(i, end = ", ")
venkata krishnan
  • 1,961
  • 1
  • 13
  • 20