How to convert Python index to list

Hello everyone,

I was hoping that you may have a way to convert a Python index [one, two, three] to a Python list [“one”,“two”,“three”]… I’m trying to create a DropDown list of all the columns in a dataset so that I can pick which ones i want to display… The problem is the data.getColumnNames() returns an index and the DropDown options is a list.

I tried the script below but it did not work:

	pds= self.view.custom.card.db_data
	headerIndex = pds.getColumnNames()		
	headerList = pds.getColumnAsList(pds.getColumnIndex(headerIndex)) 

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.

1 Like

Thank you very much for the detailed response!!! I do really appreciate t :slight_smile:
I am going to try the methods you mentioned and post the results here.

So I tried List:

pds= self.view.custom.card.db_data
headerIndex = pds.getColumnNames() #index
headerList = list(headerIndex)
system.perspective.print(headerIndex)
system.perspective.print(headerList)

I get this result:

[resource, description, name]
[u’resource’, u’description’, u’name’]

But the table turns to this:

The other column names change to column_4/5/6/7…I am not able to see the column names only I can see the column names I choose in the select dropdown. I should note that I am writing the script in the start-up event of that drop-down component.

This may help as well:

headerIndex = map(str, pds.getColumnNames())
2 Likes

Thank you very much!!!
This worked!!! :slight_smile: Appreciate your help :slight_smile: However, I could not find any information on this function map() for further using it. Could you please share any resources or anything that can be used for further issues?

Before selecting the column name the table header is like the picture below:

But as soon as I select a column name using the drop-down, the column names change to column_…

I was wondering if you have any opinion on that too?

I’m not the best one to ask about Python, but here are some links…
https://www.w3schools.com/python/ref_func_map.asp

1 Like

Are you defining the columns?
https://docs.inductiveautomation.com/display/DOC81/Perspective+-+Table

1 Like

This is all I am doing:

	#	- - - CREATE A DROP DOWN LIST OF TABLE COLUMNS - - -
	#1 GET RESOURCE DATASET


	
    pds= self.view.custom.card.db_data
	headerIndex = pds.getColumnNames()		# Index 		[myColumn]
	headerList = map(str, headerIndex)		# python List w/ ['myColumn']

		
#	3 CREATE OPTIONS LIST

	# create a new blank list that will be used to populate option
	newList = []
	# create a new blank dictionary for each row of the data
	newDict = {}
	
	items =len(headerList)
	
	for i in range(len(headerList)):
		# set name/value pairs
		newDict["label"] = headerList[i]
		newDict["value"] = headerList[i]
		newList.append(newDict)

	
#	#4 UPDATE THE COMPONENT
        self.props.options=newList

Lebel and value are for dropdown options:

image

Thank you!!!

I’m referring to the properties on the table. Are you setting field under each column?

1 Like

Right. yes I have field set up on each column

Then I’m not really sure what’s happening. Sorry.

1 Like

Not a problem at all!!! Thank you so so much for your help!!! Appreciate it.

I will say one thing, change the indent on

self.props.options=newList

I think it should be

for i in range(len(headerList)):
		# set name/value pairs
		newDict["label"] = headerList[i]
		newDict["value"] = headerList[i]
		newList.append(newDict)

	
#	#4 UPDATE THE COMPONENT
self.props.options=newList
1 Like

The indent is written in the way you mentioned here. Something must have happened when I copied it here. Thank you!