Str1=["Josephine","Joseph smith"]
Josephine should come first but due to space Joseph smith comes first.
Str1=["Josephine","Joseph smith"]
Josephine should come first but due to space Joseph smith comes first.
What you're describing is:
>>> Str1 = ['Josephine', 'Joseph smith']
>>> sorted(Str1)
['Joseph smith', 'Josephine']
You can provide a key
to list.sort
or sorted
to make your string comparisons ignore whitespace:
>>> sorted(Str1, key=lambda x: ''.join(x.split()))
['Josephine', 'Joseph smith']
You pass in a sort key that replaces all space character with an empty string:
Str1.sort(key=lambda k: k.replace(' ', ''))
You can do it this way:
l = sorted(l, key=lambda x:x.replace(' ', ''))