Just some terminology first - in both cases [one, two, three] and [“one”, “two”, “three”] - these are both lists, python doesn’t discriminate about what type is inside the list, it is still a list.
The index is what you use to get an item out of a list
l = [one, two three]
print str(l[0])
prints the string value of whatever the variable one is referring to, where 0 is the index.
If you mean you have a list like [one, two, three] and you want to cast them to strings into a new list you could do
old_list = [one, two, three]
new_list = [str(x) for x in old_list]
but mind you again the variable names one, two, three
, don’t necessarily have to line up with anything. I could do
one = 'three'
two = 'four'
three = 'six'
old_list = [one, two, three]
new_list = [str(x) for x in old_list]
and then your new list would not be what you expect.
I think in line 2
headIndex = pds.getColumnNames()
you might just need this as
headIndex = list(pds.getColumnNames()
list
is a python reserved keyword (so never ever call a variable or script list
), allowing you to type cast different types of iterables to a list
type.