Prefix + List + Suffix in Python

This is a Python question which is not specific to Ignition, but I am using this in a gateway script. I have a script that does exactly what I want but I am guessing there is a more efficient way to code this in Python and welcome any comments. I have a list of 10 alarm tags but I need to append a prefix and suffix for the tag path of each item in the list. The code is:

#Create list of integers 1-10
IndexList = range(1,11,1)
#Convert integer list to string list
StrIndexList = list(map(str, IndexList))
print StrIndexList
#Add a suffix to each value in the list
AlarmText = '/Alarm'
SuffixList = [SuffixVal + AlarmText for SuffixVal in StrIndexList]
print SuffixList
#Add a prefix to each value in the list
AlmTagList = ["Test_Tags/Alarm" + TextVal for TextVal in SuffixList]
print AlmTagList

The results from the print statements are exactly what I want:

['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
['1/Alarm', '2/Alarm', '3/Alarm', '4/Alarm', '5/Alarm', '6/Alarm', '7/Alarm', '8/Alarm', '9/Alarm', '10/Alarm']
['Test_Tags/Alarm1/Alarm', 'Test_Tags/Alarm2/Alarm', 'Test_Tags/Alarm3/Alarm', 'Test_Tags/Alarm4/Alarm', 'Test_Tags/Alarm5/Alarm', 'Test_Tags/Alarm6/Alarm', 'Test_Tags/Alarm7/Alarm', 'Test_Tags/Alarm8/Alarm', 'Test_Tags/Alarm9/Alarm', 'Test_Tags/Alarm10/Alarm']

Is there a more concise/efficient way to code this?

AlmTagList = ['Test_Tags/Alarm%d/Alarm' % x for x in range(1,11)]


AlmTagList = ["Test_Tags/Alarm%d/Alarm" % i for i in range(1,11)]

witman and ptumel, much thanks!

1 Like