+ 1

Python

Code: from itertools import permutations a = input() perms = [''.join(p) for p in permutations(a)] print(perms) Output 1 ['abc', 'acb', 'bac', 'bca', 'cab', 'cba'] Output 2 abc acb bac bca cab cba How can i make changes to my code so that the program can convert the output from Output 1 to Output 2 ?

15th Dec 2022, 4:59 AM
Levy
Levy - avatar
4 Answers
+ 5
Try this: print(*perms, sep='\n') Here * is used as the unpacking operator to pull out the list elements separately. Then sep='\n' tells print how to separate the elements while printing - in this case using a newline.
15th Dec 2022, 5:32 AM
Brian
Brian - avatar
+ 5
Levy , we can create the output without using a table. from itertools import permutations a = input() for item in permutations(a): print(''.join(item))
15th Dec 2022, 7:57 PM
Lothar
Lothar - avatar
+ 1
Brian Works for me too. Thanks
15th Dec 2022, 5:46 AM
Levy
Levy - avatar
0
Yeah, it works. Thanks
15th Dec 2022, 5:45 AM
Zoey
Zoey - avatar