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

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1eb12f6-480fc.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1eb12f6-480fc.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?