Reputation: 1
gIn = list()
egIn = [101, 'Python', 102, 'Machine Learning', 'Deep Learning', 1]
def function1 (lst1):
lst1.sort(key=lambda e: (isinstance(e, str), e))
return lst1
print(function1(egIn))
Why are we using (isinstance(e, str), e)
in this function? i understood isinstance(e,str)
but why is that other e
? what does it do and how the key
parameter is used
Upvotes: 0
Views: 77
Reputation: 2428
Here is explanation what does that piece of code do.
List method sort can have key as a parameter. It is a function which returns value and list is sorted according to that value.
(isinstance(e, str), e)
is a tuple. For string "abc"
it is (True, "abc")
and for Integer 5
it is (False, 5)
.
So it sorts first strings to the end of the list and order strings in end of the list to alphabetical order.
Upvotes: 1