Can I put something between two unpacked list in print()?

128 Views Asked by At

For example: I have a list filled with ints. I need to print it from a template(with '\n' between) with only one print():

>*sorted(lst)
>*reversed(lst)

I can't use sep or f-string because of unpacking lists. I found a bruteforce method.

int_lst = [4, 5, 2, 1]
lst = sorted(int_lst)
print(" ".join(str(i) for i in lst) + "\n" + " ".join(str(j) for j in reversed(lst)))

>1 2 4 5
>5 4 2 1

Isn't there any way to make it prettier and shorter?

1

There are 1 best solutions below

1
On

My code sorts a list of integers and prints the sorted list twice, once in ascending order and once in descending order, with a newline between them.

int_lst = [4, 5, 2, 1]

sorted_lst = sorted(int_lst)
print('\n'.join([' '.join(map(str, sorted_lst)), ' '.join(map(str, reversed(sorted_lst)))]))