Your code is fine except for the printing part. You were printing too many spaces in between and one extra space at the end.
I have made a submission using your code with a small modification at the end.
This SO should help: python - Print a list of space-separated elements - Stack Overflow