Ask the question of Python list generation

L = ["Hello", "World", 18, "Apple", None]

print(
    [s.lower() for s in L if isinstance(s, str)]
)

the above code changes all strings in a list to lowercase
output: ["hello"," world", "apple"]

the result I expect is ["hello"," world", 18, "apple", None]

that is to keep all the contents in it. What should I do

Nov.03,2021

because you add if isinstance (s, str) and you drop all the non-string Filter, you can put the condition first:

[s.lower() if isinstance(s, str) else s for s in L]
< hr >

add that although python's list supports any type, I personally think that all elements in list should be of the same type

Menu