Converting list of both "word strings" and "number strings" to a list where the "number strings" are converted to just numbers

Viewed 320

I realize my wording for the title isn't the best, but I hope an example will clear this up.

How would I convert a list like

example_list = ["asdf" , "4", "asdfasdf" , "8" , "9" ,"asdf"]

to a list like

converted_list = ["asdf" , 4, "asdfasdf", 8 , 9 , "asdf"]

So basically how do I make a list where strings that can be converted to integers are converted to integers while strings that cannot be converted remain as strings?

As a side note, how would I afterwards test in a for loop if each item in the converted_list is an integer or not?

The context for this issue is that I am trying to convert headers in pandas to integers if possible, since all the integers are strings as of now. And then if the column had a stringed number as a header, I would take the mean of the column. Right now, I have made all the headers into a list.

2 Answers
Related