0
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

2
  • Please use the code tag to add the code part. Commented Mar 11, 2021 at 6:40
  • Does this answer your question? What is key=lambda Commented Mar 11, 2021 at 6:56

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.