Conversion of the list of two strings & ldquo; & rdquo; and & ldquo; number strings & rdquo; to a list where the & ldquo; number strings & rdquo; are converted to just numbers

advertisements

This question already has an answer here:

  • How to convert strings numbers to integers in a list? 4 answers

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.


You can use a list comprehension with a ternary to determine whether or not each element of the list is a number.

>>> [int(n) if n.isdigit() else n for n in example_list]
['asdf', 4, 'asdfasdf', 8, 9, 'asdf']