May
23
How to detect a valid integer literal
There are hundreds of questions on StackOverflow that all ask variations of the same thing. Paraphrasing:
lst is a list of strings and numbers. I want to convert the numbers to int but leave the strings alone. How do I do that? This immediately gets a half-dozen answers that all do some equivalent of:
lst = [int(x) if x.isdigit() else x for x in lst] This has a number of problems, but they all come down to the same two:
"Numbers" is vague.
lst is a list of strings and numbers. I want to convert the numbers to int but leave the strings alone. How do I do that? This immediately gets a half-dozen answers that all do some equivalent of:
lst = [int(x) if x.isdigit() else x for x in lst] This has a number of problems, but they all come down to the same two:
"Numbers" is vague.