-5

I have a list of string elements where list = [X, Y, Z].

I want to update the elements in the list such that it becomes [X_AAA, X_BBB, X_CCC, Y_AAA, Y_BBB, Y_CCC, Z_AAA, Z_BBB, Z_CCC]. Essentially, I'm appending AAA, BBB and CCC to every element in this list of strings.

What is the most efficient way to do this?

Thank you!

.

jo_
  • 677
  • 2
  • 11

1 Answers1

2

I would do it like this:

import itertools

prefixes = ['X', 'Y', 'Z']
suffixes = ['AAA', 'BBB', 'CCC']
result = [f'{prefix}_{suffix}' for prefix, suffix in itertools.product(prefixes, suffixes)]
0x5453
  • 12,753
  • 1
  • 32
  • 61