If... else skip

Cameron and Vladimir have very good points. If the sample solution is from the course authors I would stay away from such a course or at least I would be very cautious!

That is the reason why I wrote:

It is the part in the official documentation which explains the problem.

Though Cameron and Vladimir did not mention that in this case (your and the sample code) the code will work correctly because input_list is only being read in the function. A problem would be if it was modified. Anyway such a sample shows very bad example without an explanation when just a slight modification would make it better (though still needing an explanation):

def unique(input_list=()):
    # input_list is not being modified inside the function so we can provide the default
    # value right in the function arguments definition. To express the intent
    # to not modify the value we use a tuple (immutable) instead of a list (mutable).
    ...

new_stuff = list(x.lower() if isinstance(x, str) else (x) for x in random)

Why are you still creating a list immediately from a generator expression? This is complicating the code and somewhat obscuring your intent. Use a list comprehension which exists for this use:

new_stuff = [x.lower() if isinstance(x, str) else x for x in random]

I also removed the brackets around x for which a saw no reason. To make the code more readable (the conditional expression lowers the readability) you can split it to lines:

new_stuff = [
    x.lower() if isinstance(x, str) else x
    for x in random]
1 Like