How to double string stored at an even index and triple string stored at an index that is multiple of 3

enter image description hereBelow is the scenario.

Write a function called multiply_strings. Multiply strings should have one parameter, a list of strings. It should return a modified list according to the following changes:

  • Every string stored at an even index should be doubled.
  • Every string stored at an index that is a multiple of 3 should be tripled.
  • Every other string should remain unchanged.

These changes should “stack”

test_list = [“A”, “B”, “C”, “D”, “E”, “F”, “G”]

If your function works correctly, this will originally print: [‘AAAAAA’, ‘B’, ‘CC’, ‘DDD’, ‘EE’, ‘F’, ‘GGGGGG’]

def multiply_strings(test_list): 
    for i in test_list:
        print(i) 

test_list = ["A", "B", "C", "D", "E", "F", "G"] print(multiply_strings(test_list))

Answer

The comments already covered it but I thought I would try to put an easier to understand answer down here.

def multiply_strings(test_list):
    for i, _ in enumerate(test_list):

        # 2 if statements are required for changes to stack
        if not i % 2:  # even
            test_list[i] *= 2

        if not i % 3:  # multiple of 3
            test_list[i] *= 3

    return test_list


test_list = ["A", "B", "C", "D", "E", "F", "G"]

print(multiply_strings(test_list))

*= is the same idea as +=, it just takes the item itself and multiplies by the number on the right.

Note that instead of enumerate, you could technically just use for i in range(len(test_list)).