0

I am trying to find an optimal way to execute the below loop statement

I have the below code that works just fine where in I check for length of a column and perform actions accordingly. However I am unable to optimise the code but have to create this long if statements

Given below is the code I have:

    if len(name) == 1:
        table.cell(1, 0).text = str(type_1_1)
        table.cell(1, 1).text = str(type_1_2)
        table.cell(1, 2).text = str(type_1_3)
        table.cell(1, 3).text = str(type_1_4)
    elif len(name) == 2:
        table.cell(1, 0).text = str(type_1_1)
        table.cell(1, 1).text = str(type_1_2)
        table.cell(1, 2).text = str(type_1_3)
        table.cell(1, 3).text = str(type_1_4)
        table.cell(2, 0).text = str(type_2_1)
        table.cell(2, 1).text = str(type_2_2)
        table.cell(2, 2).text = str(type_2_3)
        table.cell(2, 3).text = str(type_2_4)
    elif len(name) == 3:
        table.cell(1, 0).text = str(type_1_1)
        table.cell(1, 1).text = str(type_1_2)
        table.cell(1, 2).text = str(type_1_3)
        table.cell(1, 3).text = str(type_1_4)
        table.cell(2, 0).text = str(type_2_1)
        table.cell(2, 1).text = str(type_2_2)
        table.cell(2, 2).text = str(type_2_3)
        table.cell(2, 3).text = str(type_2_4)
        table.cell(3, 0).text = str(type_3_1)
        table.cell(3, 1).text = str(type_3_2)
        table.cell(3, 2).text = str(type_3_3)
        table.cell(3, 3).text = str(type_3_4)
    elif len(name) == 4:
        table.cell(1, 0).text = str(type_1_1)
        table.cell(1, 1).text = str(type_1_2)
        table.cell(1, 2).text = str(type_1_3)
        table.cell(1, 3).text = str(type_1_4)
        table.cell(2, 0).text = str(type_2_1)
        table.cell(2, 1).text = str(type_2_2)
        table.cell(2, 2).text = str(type_2_3)
        table.cell(2, 3).text = str(type_2_4)
        table.cell(3, 0).text = str(type_3_1)
        table.cell(3, 1).text = str(type_3_2)
        table.cell(3, 2).text = str(type_3_3)
        table.cell(3, 3).text = str(type_3_4)
        table.cell(4, 0).text = str(type_4_1)
        table.cell(4, 1).text = str(type_4_2)
        table.cell(4, 2).text = str(type_4_3)
        table.cell(4, 3).text = str(type_4_4)
    elif len(name) == 5:
        table.cell(1, 0).text = str(type_1_1)
        table.cell(1, 1).text = str(type_1_2)
        table.cell(1, 2).text = str(type_1_3)
        table.cell(1, 3).text = str(type_1_4)
        table.cell(2, 0).text = str(type_2_1)
        table.cell(2, 1).text = str(type_2_2)
        table.cell(2, 2).text = str(type_2_3)
        table.cell(2, 3).text = str(type_2_4)
        table.cell(3, 0).text = str(type_3_1)
        table.cell(3, 1).text = str(type_3_2)
        table.cell(3, 2).text = str(type_3_3)
        table.cell(3, 3).text = str(type_3_4)
        table.cell(4, 0).text = str(type_4_1)
        table.cell(4, 1).text = str(type_4_2)
        table.cell(4, 2).text = str(type_4_3)
        table.cell(4, 3).text = str(type_4_4)
        table.cell(5, 0).text = str(type_5_1)
        table.cell(5, 1).text = str(type_5_2)
        table.cell(5, 2).text = str(type_5_3)
        table.cell(5, 3).text = str(type_5_4)
    elif len(name) == 6:
        table.cell(1, 0).text = str(type_1_1)
        table.cell(1, 1).text = str(type_1_2)
        table.cell(1, 2).text = str(type_1_3)
        table.cell(1, 3).text = str(type_1_4)
        table.cell(2, 0).text = str(type_2_1)
        table.cell(2, 1).text = str(type_2_2)
        table.cell(2, 2).text = str(type_2_3)
        table.cell(2, 3).text = str(type_2_4)
        table.cell(3, 0).text = str(type_3_1)
        table.cell(3, 1).text = str(type_3_2)
        table.cell(3, 2).text = str(type_3_3)
        table.cell(3, 3).text = str(type_3_4)
        table.cell(4, 0).text = str(type_4_1)
        table.cell(4, 1).text = str(type_4_2)
        table.cell(4, 2).text = str(type_4_3)
        table.cell(4, 3).text = str(type_4_4)
        table.cell(5, 0).text = str(type_5_1)
        table.cell(5, 1).text = str(type_5_2)
        table.cell(5, 2).text = str(type_5_3)
        table.cell(5, 3).text = str(type_5_4)
        table.cell(6, 0).text = str(type_6_1)
        table.cell(6, 1).text = str(type_6_2)
        table.cell(6, 2).text = str(type_6_3)
        table.cell(6, 3).text = str(type_6_4)
    elif len(name) == 7:
        table.cell(1, 0).text = str(type_1_1)
        table.cell(1, 1).text = str(type_1_2)
        table.cell(1, 2).text = str(type_1_3)
        table.cell(1, 3).text = str(type_1_4)
        table.cell(2, 0).text = str(type_2_1)
        table.cell(2, 1).text = str(type_2_2)
        table.cell(2, 2).text = str(type_2_3)
        table.cell(2, 3).text = str(type_2_4)
        table.cell(3, 0).text = str(type_3_1)
        table.cell(3, 1).text = str(type_3_2)
        table.cell(3, 2).text = str(type_3_3)
        table.cell(3, 3).text = str(type_3_4)
        table.cell(4, 0).text = str(type_4_1)
        table.cell(4, 1).text = str(type_4_2)
        table.cell(4, 2).text = str(type_4_3)
        table.cell(4, 3).text = str(type_4_4)
        table.cell(5, 0).text = str(type_5_1)
        table.cell(5, 1).text = str(type_5_2)
        table.cell(5, 2).text = str(type_5_3)
        table.cell(5, 3).text = str(type_5_4)
        table.cell(6, 0).text = str(type_6_1)
        table.cell(6, 1).text = str(type_6_2)
        table.cell(6, 2).text = str(type_6_3)
        table.cell(6, 3).text = str(type_6_4)
        table.cell(7, 0).text = str(type_7_1)
        table.cell(7, 1).text = str(type_7_2)
        table.cell(7, 2).text = str(type_7_3)
        table.cell(7, 3).text = str(type_7_4)
    elif len(name) == 8:
        table.cell(1, 0).text = str(type_1_1)
        table.cell(1, 1).text = str(type_1_2)
        table.cell(1, 2).text = str(type_1_3)
        table.cell(1, 3).text = str(type_1_4)
        table.cell(2, 0).text = str(type_2_1)
        table.cell(2, 1).text = str(type_2_2)
        table.cell(2, 2).text = str(type_2_3)
        table.cell(2, 3).text = str(type_2_4)
        table.cell(3, 0).text = str(type_3_1)
        table.cell(3, 1).text = str(type_3_2)
        table.cell(3, 2).text = str(type_3_3)
        table.cell(3, 3).text = str(type_3_4)
        table.cell(4, 0).text = str(type_4_1)
        table.cell(4, 1).text = str(type_4_2)
        table.cell(4, 2).text = str(type_4_3)
        table.cell(4, 3).text = str(type_4_4)
        table.cell(5, 0).text = str(type_5_1)
        table.cell(5, 1).text = str(type_5_2)
        table.cell(5, 2).text = str(type_5_3)
        table.cell(5, 3).text = str(type_5_4)
        table.cell(6, 0).text = str(type_6_1)
        table.cell(6, 1).text = str(type_6_2)
        table.cell(6, 2).text = str(type_6_3)
        table.cell(6, 3).text = str(type_6_4)
        table.cell(7, 0).text = str(type_7_1)
        table.cell(7, 1).text = str(type_7_2)
        table.cell(7, 2).text = str(type_7_3)
        table.cell(7, 3).text = str(type_7_4)
        table.cell(8, 0).text = str(type_8_1)
        table.cell(8, 1).text = str(type_8_2)
        table.cell(8, 2).text = str(type_8_3)
        table.cell(8, 3).text = str(type_8_4)
    elif len(name) == 9:
        table.cell(1, 0).text = str(type_1_1)
        table.cell(1, 1).text = str(type_1_2)
        table.cell(1, 2).text = str(type_1_3)
        table.cell(1, 3).text = str(type_1_4)
        table.cell(2, 0).text = str(type_2_1)
        table.cell(2, 1).text = str(type_2_2)
        table.cell(2, 2).text = str(type_2_3)
        table.cell(2, 3).text = str(type_2_4)
        table.cell(3, 0).text = str(type_3_1)
        table.cell(3, 1).text = str(type_3_2)
        table.cell(3, 2).text = str(type_3_3)
        table.cell(3, 3).text = str(type_3_4)
        table.cell(4, 0).text = str(type_4_1)
        table.cell(4, 1).text = str(type_4_2)
        table.cell(4, 2).text = str(type_4_3)
        table.cell(4, 3).text = str(type_4_4)
        table.cell(5, 0).text = str(type_5_1)
        table.cell(5, 1).text = str(type_5_2)
        table.cell(5, 2).text = str(type_5_3)
        table.cell(5, 3).text = str(type_5_4)
        table.cell(6, 0).text = str(type_6_1)
        table.cell(6, 1).text = str(type_6_2)
        table.cell(6, 2).text = str(type_6_3)
        table.cell(6, 3).text = str(type_6_4)
        table.cell(7, 0).text = str(type_7_1)
        table.cell(7, 1).text = str(type_7_2)
        table.cell(7, 2).text = str(type_7_3)
        table.cell(7, 3).text = str(type_7_4)
        table.cell(8, 0).text = str(type_8_1)
        table.cell(8, 1).text = str(type_8_2)
        table.cell(8, 2).text = str(type_8_3)
        table.cell(8, 3).text = str(type_8_4)
        table.cell(9, 0).text = str(type_9_1)
        table.cell(9, 1).text = str(type_9_2)
        table.cell(9, 2).text = str(type_9_3)
        table.cell(9, 3).text = str(type_9_4)
    elif len(name) == 10:
        table.cell(1, 0).text = str(type_1_1)
        table.cell(1, 1).text = str(type_1_2)
        table.cell(1, 2).text = str(type_1_3)
        table.cell(1, 3).text = str(type_1_4)
        table.cell(2, 0).text = str(type_2_1)
        table.cell(2, 1).text = str(type_2_2)
        table.cell(2, 2).text = str(type_2_3)
        table.cell(2, 3).text = str(type_2_4)
        table.cell(3, 0).text = str(type_3_1)
        table.cell(3, 1).text = str(type_3_2)
        table.cell(3, 2).text = str(type_3_3)
        table.cell(3, 3).text = str(type_3_4)
        table.cell(4, 0).text = str(type_4_1)
        table.cell(4, 1).text = str(type_4_2)
        table.cell(4, 2).text = str(type_4_3)
        table.cell(4, 3).text = str(type_4_4)
        table.cell(5, 0).text = str(type_5_1)
        table.cell(5, 1).text = str(type_5_2)
        table.cell(5, 2).text = str(type_5_3)
        table.cell(5, 3).text = str(type_5_4)
        table.cell(6, 0).text = str(type_6_1)
        table.cell(6, 1).text = str(type_6_2)
        table.cell(6, 2).text = str(type_6_3)
        table.cell(6, 3).text = str(type_6_4)
        table.cell(7, 0).text = str(type_7_1)
        table.cell(7, 1).text = str(type_7_2)
        table.cell(7, 2).text = str(type_7_3)
        table.cell(7, 3).text = str(type_7_4)
        table.cell(8, 0).text = str(type_8_1)
        table.cell(8, 1).text = str(type_8_2)
        table.cell(8, 2).text = str(type_8_3)
        table.cell(8, 3).text = str(type_8_4)
        table.cell(9, 0).text = str(type_9_1)
        table.cell(9, 1).text = str(type_9_2)
        table.cell(9, 2).text = str(type_9_3)
        table.cell(9, 3).text = str(type_9_4)
        table.cell(10, 0).text = str(type_10_1)
        table.cell(10, 1).text = str(type_10_2)
        table.cell(10, 2).text = str(type_10_3)
        table.cell(10, 3).text = str(type_10_4)
    elif len(name) == 11:
        table.cell(1, 0).text = str(type_1_1)
        table.cell(1, 1).text = str(type_1_2)
        table.cell(1, 2).text = str(type_1_3)
        table.cell(1, 3).text = str(type_1_4)
        table.cell(2, 0).text = str(type_2_1)
        table.cell(2, 1).text = str(type_2_2)
        table.cell(2, 2).text = str(type_2_3)
        table.cell(2, 3).text = str(type_2_4)
        table.cell(3, 0).text = str(type_3_1)
        table.cell(3, 1).text = str(type_3_2)
        table.cell(3, 2).text = str(type_3_3)
        table.cell(3, 3).text = str(type_3_4)
        table.cell(4, 0).text = str(type_4_1)
        table.cell(4, 1).text = str(type_4_2)
        table.cell(4, 2).text = str(type_4_3)
        table.cell(4, 3).text = str(type_4_4)
        table.cell(5, 0).text = str(type_5_1)
        table.cell(5, 1).text = str(type_5_2)
        table.cell(5, 2).text = str(type_5_3)
        table.cell(5, 3).text = str(type_5_4)
        table.cell(6, 0).text = str(type_6_1)
        table.cell(6, 1).text = str(type_6_2)
        table.cell(6, 2).text = str(type_6_3)
        table.cell(6, 3).text = str(type_6_4)
        table.cell(7, 0).text = str(type_7_1)
        table.cell(7, 1).text = str(type_7_2)
        table.cell(7, 2).text = str(type_7_3)
        table.cell(7, 3).text = str(type_7_4)
        table.cell(8, 0).text = str(type_8_1)
        table.cell(8, 1).text = str(type_8_2)
        table.cell(8, 2).text = str(type_8_3)
        table.cell(8, 3).text = str(type_8_4)
        table.cell(9, 0).text = str(type_9_1)
        table.cell(9, 1).text = str(type_9_2)
        table.cell(9, 2).text = str(type_9_3)
        table.cell(9, 3).text = str(type_9_4)
        table.cell(10, 0).text = str(type_10_1)
        table.cell(10, 1).text = str(type_10_2)
        table.cell(10, 2).text = str(type_10_3)
        table.cell(10, 3).text = str(type_10_4)
        table.cell(11, 0).text = str(type_11_1)
        table.cell(11, 1).text = str(type_11_2)
        table.cell(11, 2).text = str(type_11_3)
        table.cell(11, 3).text = str(type_11_4)
    elif len(name) == 12:
        table.cell(1, 0).text = str(type_1_1)
        table.cell(1, 1).text = str(type_1_2)
        table.cell(1, 2).text = str(type_1_3)
        table.cell(1, 3).text = str(type_1_4)
        table.cell(2, 0).text = str(type_2_1)
        table.cell(2, 1).text = str(type_2_2)
        table.cell(2, 2).text = str(type_2_3)
        table.cell(2, 3).text = str(type_2_4)
        table.cell(3, 0).text = str(type_3_1)
        table.cell(3, 1).text = str(type_3_2)
        table.cell(3, 2).text = str(type_3_3)
        table.cell(3, 3).text = str(type_3_4)
        table.cell(4, 0).text = str(type_4_1)
        table.cell(4, 1).text = str(type_4_2)
        table.cell(4, 2).text = str(type_4_3)
        table.cell(4, 3).text = str(type_4_4)
        table.cell(5, 0).text = str(type_5_1)
        table.cell(5, 1).text = str(type_5_2)
        table.cell(5, 2).text = str(type_5_3)
        table.cell(5, 3).text = str(type_5_4)
        table.cell(6, 0).text = str(type_6_1)
        table.cell(6, 1).text = str(type_6_2)
        table.cell(6, 2).text = str(type_6_3)
        table.cell(6, 3).text = str(type_6_4)
        table.cell(7, 0).text = str(type_7_1)
        table.cell(7, 1).text = str(type_7_2)
        table.cell(7, 2).text = str(type_7_3)
        table.cell(7, 3).text = str(type_7_4)
        table.cell(8, 0).text = str(type_8_1)
        table.cell(8, 1).text = str(type_8_2)
        table.cell(8, 2).text = str(type_8_3)
        table.cell(8, 3).text = str(type_8_4)
        table.cell(9, 0).text = str(type_9_1)
        table.cell(9, 1).text = str(type_9_2)
        table.cell(9, 2).text = str(type_9_3)
        table.cell(9, 3).text = str(type_9_4)
        table.cell(10, 0).text = str(type_10_1)
        table.cell(10, 1).text = str(type_10_2)
        table.cell(10, 2).text = str(type_10_3)
        table.cell(10, 3).text = str(type_10_4)
        table.cell(11, 0).text = str(type_11_1)
        table.cell(11, 1).text = str(type_11_2)
        table.cell(11, 2).text = str(type_11_3)
        table.cell(11, 3).text = str(type_11_4)
        table.cell(12, 0).text = str(type_12_1)
        table.cell(12, 1).text = str(type_12_2)
        table.cell(12, 2).text = str(type_12_3)
        table.cell(12, 3).text = str(type_12_4)
    elif len(name) == 13:
        table.cell(1, 0).text = str(type_1_1)
        table.cell(1, 1).text = str(type_1_2)
        table.cell(1, 2).text = str(type_1_3)
        table.cell(1, 3).text = str(type_1_4)
        table.cell(2, 0).text = str(type_2_1)
        table.cell(2, 1).text = str(type_2_2)
        table.cell(2, 2).text = str(type_2_3)
        table.cell(2, 3).text = str(type_2_4)
        table.cell(3, 0).text = str(type_3_1)
        table.cell(3, 1).text = str(type_3_2)
        table.cell(3, 2).text = str(type_3_3)
        table.cell(3, 3).text = str(type_3_4)
        table.cell(4, 0).text = str(type_4_1)
        table.cell(4, 1).text = str(type_4_2)
        table.cell(4, 2).text = str(type_4_3)
        table.cell(4, 3).text = str(type_4_4)
        table.cell(5, 0).text = str(type_5_1)
        table.cell(5, 1).text = str(type_5_2)
        table.cell(5, 2).text = str(type_5_3)
        table.cell(5, 3).text = str(type_5_4)
        table.cell(6, 0).text = str(type_6_1)
        table.cell(6, 1).text = str(type_6_2)
        table.cell(6, 2).text = str(type_6_3)
        table.cell(6, 3).text = str(type_6_4)
        table.cell(7, 0).text = str(type_7_1)
        table.cell(7, 1).text = str(type_7_2)
        table.cell(7, 2).text = str(type_7_3)
        table.cell(7, 3).text = str(type_7_4)
        table.cell(8, 0).text = str(type_8_1)
        table.cell(8, 1).text = str(type_8_2)
        table.cell(8, 2).text = str(type_8_3)
        table.cell(8, 3).text = str(type_8_4)
        table.cell(9, 0).text = str(type_9_1)
        table.cell(9, 1).text = str(type_9_2)
        table.cell(9, 2).text = str(type_9_3)
        table.cell(9, 3).text = str(type_9_4)
        table.cell(10, 0).text = str(type_10_1)
        table.cell(10, 1).text = str(type_10_2)
        table.cell(10, 2).text = str(type_10_3)
        table.cell(10, 3).text = str(type_10_4)
        table.cell(11, 0).text = str(type_11_1)
        table.cell(11, 1).text = str(type_11_2)
        table.cell(11, 2).text = str(type_11_3)
        table.cell(11, 3).text = str(type_11_4)
        table.cell(12, 0).text = str(type_12_1)
        table.cell(12, 1).text = str(type_12_2)
        table.cell(12, 2).text = str(type_12_3)
        table.cell(12, 3).text = str(type_12_4)
        table.cell(13, 0).text = str(type_13_1)
        table.cell(13, 1).text = str(type_13_2)
        table.cell(13, 2).text = str(type_13_3)
        table.cell(13, 3).text = str(type_13_4)
    elif len(name) == 14:
        table.cell(1, 0).text = str(type_1_1)
        table.cell(1, 1).text = str(type_1_2)
        table.cell(1, 2).text = str(type_1_3)
        table.cell(1, 3).text = str(type_1_4)
        table.cell(2, 0).text = str(type_2_1)
        table.cell(2, 1).text = str(type_2_2)
        table.cell(2, 2).text = str(type_2_3)
        table.cell(2, 3).text = str(type_2_4)
        table.cell(3, 0).text = str(type_3_1)
        table.cell(3, 1).text = str(type_3_2)
        table.cell(3, 2).text = str(type_3_3)
        table.cell(3, 3).text = str(type_3_4)
        table.cell(4, 0).text = str(type_4_1)
        table.cell(4, 1).text = str(type_4_2)
        table.cell(4, 2).text = str(type_4_3)
        table.cell(4, 3).text = str(type_4_4)
        table.cell(5, 0).text = str(type_5_1)
        table.cell(5, 1).text = str(type_5_2)
        table.cell(5, 2).text = str(type_5_3)
        table.cell(5, 3).text = str(type_5_4)
        table.cell(6, 0).text = str(type_6_1)
        table.cell(6, 1).text = str(type_6_2)
        table.cell(6, 2).text = str(type_6_3)
        table.cell(6, 3).text = str(type_6_4)
        table.cell(7, 0).text = str(type_7_1)
        table.cell(7, 1).text = str(type_7_2)
        table.cell(7, 2).text = str(type_7_3)
        table.cell(7, 3).text = str(type_7_4)
        table.cell(8, 0).text = str(type_8_1)
        table.cell(8, 1).text = str(type_8_2)
        table.cell(8, 2).text = str(type_8_3)
        table.cell(8, 3).text = str(type_8_4)
        table.cell(9, 0).text = str(type_9_1)
        table.cell(9, 1).text = str(type_9_2)
        table.cell(9, 2).text = str(type_9_3)
        table.cell(9, 3).text = str(type_9_4)
        table.cell(10, 0).text = str(type_10_1)
        table.cell(10, 1).text = str(type_10_2)
        table.cell(10, 2).text = str(type_10_3)
        table.cell(10, 3).text = str(type_10_4)
        table.cell(11, 0).text = str(type_11_1)
        table.cell(11, 1).text = str(type_11_2)
        table.cell(11, 2).text = str(type_11_3)
        table.cell(11, 3).text = str(type_11_4)
        table.cell(12, 0).text = str(type_12_1)
        table.cell(12, 1).text = str(type_12_2)
        table.cell(12, 2).text = str(type_12_3)
        table.cell(12, 3).text = str(type_12_4)
        table.cell(13, 0).text = str(type_13_1)
        table.cell(13, 1).text = str(type_13_2)
        table.cell(13, 2).text = str(type_13_3)
        table.cell(13, 3).text = str(type_13_4)
        table.cell(14, 0).text = str(type_14_1)
        table.cell(14, 1).text = str(type_14_2)
        table.cell(14, 2).text = str(type_14_3)
        table.cell(14, 3).text = str(type_14_4)
8
  • 1
    Think about replacing if len(name) == 1: with if len(name) >= 1: (not just for 1, but for all the other numbers as well). Then for each larger number you will only have to add the new cells. Commented Feb 11, 2019 at 6:20
  • What is type_1_1? Commented Feb 11, 2019 at 6:21
  • @user5173426 its a variable that is assigned a value Commented Feb 11, 2019 at 6:22
  • 1
    You should not have stored your values in so many oddly named variables in the first place. Commented Feb 11, 2019 at 6:31
  • 3
    I'm having trouble seeing how this question is anything other than "how do I create a loop in Python?". Commented Feb 11, 2019 at 6:34

3 Answers 3

1

It appears that all your values build on each other. That is, the work done for length N is equal to the work done for length N-1, plus one more bit of work. With that in mind, consider making your if statements non-exclusive, like so:

    if len(name) >= 1:
        table.cell(1, 0).text = str(type_1_1)
        table.cell(1, 1).text = str(type_1_2)
        table.cell(1, 2).text = str(type_1_3)
        table.cell(1, 3).text = str(type_1_4)

    # Note: NOT elif, just if
    if len(name) >= 2:
        table.cell(2, 0).text = str(type_2_1)
        table.cell(2, 1).text = str(type_2_2)
        table.cell(2, 2).text = str(type_2_3)
        table.cell(2, 3).text = str(type_2_4)

    if len(name) == 3:
        table.cell(3, 0).text = str(type_3_1)
        table.cell(3, 1).text = str(type_3_2)
        table.cell(3, 2).text = str(type_3_3)
        table.cell(3, 3).text = str(type_3_4)

Now there is a nice repeating pattern. The next problem is that the various type_X_Y variables are stored as separate names instead of using a list or dictionary. There are a couple of hacky things that could be done, but let's just ignore that until you give more data. Instead, let's create a function that sets one row of table cell data:

def set_table_row(table, rownum, *values):
    assert len(values) == 4, "4 values are required per row"

    for col, value in enumerate(values):
        table.cell(rownum, col) = str(value)

Now you can collapse the 4 statements in each block into a single helper-function call:

    if len(name) >= 1:
        set_table_row(table, 1, type_1_1, type_1_2, type_1_3, type_1_4)
    if len(name) >= 2:
        set_table_row(table, 2, type_2_1, type_2_2, type_2_3, type_2_4)
    if len(name) == 3:
        set_table_row(table, 3, type_3_1, type_3_2, type_3_3, type_3_4)

This might be enough for you. If not, you'll need to provide more information about what the various type_X_Y values are: are they global or local variables, properties on an object, values extracted from a tuple?

Sign up to request clarification or add additional context in comments.

4 Comments

assert should not be used for control flow, as assertions can be turned off.
Aside from appealing to locals(), this seems to be the most reasonable solution.
It's not clear to me what the objection to assert is. I'm not doing control flow, I'm requiring a certain number of arguments and reminding the coder of that requirement.
That requirement and reminder may not show up. assert is for testing.
0

Consider using a multi-dimensional array for values or a dictionary instead of custom variables, but you can do it like this:

for x in range(len(name)):
    table.cell(x, 0).text = str(locals()["type_" + x + "_1"])
    table.cell(x, 1).text = str(locals()["type_" + x + "_2"])
    table.cell(x, 2).text = str(locals()["type_" + x + "_3"])
    table.cell(x, 3).text = str(locals()["type_" + x + "_4"])

Or another loop:

for i in range(len(name)):
    for j in range(4):
        table.cell(i, j).text = str(locals()["type_" + i + "_" + j])

Multidimensional array could be done like this:

types = [[]]
types[0][0] = "foo"
types[0][1] = "bar"
# ...
types[1][0] = ".."

for i in range(len(name)):
    for j in range(4):
        table.cell(i, j).text = types[i][j]

4 Comments

Using locals() is a terrible solution that just encourages a pile of individual variables and should not be recommended.
@TigerhawkT3 Of course not, that's why I recommend a multi-dimensional array or dictionary. But given the choice of this many local variables, using locals() is by far the most maintainable solution.
Luckily, nobody recommended eval() so far :)
Your recommendation of a real solution is a fragment of a sentence, while your locals() solution is fully expressed in two different ways. And that's not a maintainable solution at all; that's why it "of course" should not be recommended in the first place. This answer is harmful.
0

You can use iterators:

from itertools import cycle, count

types_ = ((k, v) for k, v in dict(locals()).items() if k.startswith('type_'))
types = [type for _, type in sorted(types_)]

### Alternatively you can define the list with types manually
### types = [type_1_1, type_1_2, type_1_3, type_1_4...]

def func(name):
    # assert len(name) <= len(types)
    r = (i for i in count(start=1) for _ in range(4))
    c = cycle(range(4))
    t = iter(types)

    for _ in range(len(name) * 4):
        table.cell(next(r), next(c)).text = str(next(t))

Example:

types = [f'type_{i}_{j}' for i in range(1, 100) for j in range(1, 5)]
# ['type_1_1', 'type_1_2', 'type_1_3', 'type_1_4', 'type_2_1', 'type_2_2', 'type_2_3', 'type_2_4'...]

def func(name):
    r = (i for i in count(start=1) for _ in range(4))
    c = cycle(range(4))
    t = iter(types)

    for _ in range(len(name) * 4):
        print(f'table.cell({next(r)}, {next(c)}).text = str({next(t)})')

func('AB')

Output:

table.cell(1, 0).text = str(type_1_1)
table.cell(1, 1).text = str(type_1_2)
table.cell(1, 2).text = str(type_1_3)
table.cell(1, 3).text = str(type_1_4)
table.cell(2, 0).text = str(type_2_1)
table.cell(2, 1).text = str(type_2_2)
table.cell(2, 2).text = str(type_2_3)
table.cell(2, 3).text = str(type_2_4)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.