What I'm looking for is the best way to say, 'If this list is too short, lengthen it to 9 elements and add 'Choice 4', 'Choice 5', etc, as the additional elements. Also, replace any 'None' elements with 'Choice x'.' It is ok to replace "" and 0 too.
An example transformation would be
['a','b','Choice 3','c','Choice 5','Choice 6','Choice 7','Choice 8','Choice 9']
for i in range(0,9):
if cList[i] is None:
cList[i] = "Choice %d"%(i+1)
# Fill in any blank entries
for i, v in enumerate(cList):
cList[i] = v or "Choice %s" % (i+1)
# Extend the list to 9 choices
for j in range(len(cList)+1, 10):
cList.append("Choice %s" % (j))
My initial reaction was to split the list extension and "filling in the blanks" into separate parts as so:
for i, v in enumerate(my_list): my_list[i] = v or "Choice %s" % (i+1) for j in range(len(my_list)+1, 10): my_list.append("Choice %s" % (j)) # maybe this is nicer for the extension? while len(my_list) < 10: my_list.append("Choice %s" % (len(my_list)+1))
If you do stick with the
try...except approach, do catch a specific exception as Douglas shows. Otherwise, you'll catch everything:
SyntaxErrors, ... . You do not want to do that.
EDIT: fixed 1-indexed list error - thanks DNS!
EDIT: added alternative list extension