Humoyun Humoyun - 1 year ago 127
Python Question

How to convert list into string without loop and join() in Python

I am given a task to sort list like shown below:

Input: "Sorting1234"
Output: "ginortS1324"

without using join(), for or while anywhere in the code.
I succeeded after a lot of tries to sort in the required way, but I am unable to print it as a string

My Output is: ['g', 'i', 'n', 'o', 'r', 't', 'S', '1', '3', '2', '4']

Here is my algorithm to sort with sorted():

def iseven(x):
if x.isdigit():
return int(x)+9 if int(x)%2==0 else int(x)
res=sorted(st, key=lambda x: (x.isdigit(), x.isupper(), iseven(x), ord(x) ))

Please help me on this

Answer Source

but I am unable to print it as a string

Just unpack the arguments out of the list using the * operator when calling print() and use "" as a separator:

>>> L = ['g', 'i', 'n', 'o', 'r', 't', 'S', '1', '3', '2', '4']
>>> print(*L, sep="")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download