0

I have

A = ['A','B','C','D','E','F','G','H']
B= ['a','b']

want to iterate through the list to get

C = ['Aa','Bb','Ca','Db','Ea','Fb','Ga','Hb']

how can I go about it in python

3
  • 1
    What is 1a supposed to be? Do you mean '1a' string? Commented May 4, 2022 at 9:15
  • you just make a loop into a loop Commented May 4, 2022 at 9:16
  • @ElPandario double loops are unnecessary here. Commented May 4, 2022 at 9:16

6 Answers 6

2

You can use a list comprehension :

a = ['A','B','C','D','E','F','G','H']
b = ['a','b']

c = [a[i] + b[i%2] for i in range(len(a))]

# ['Aa', 'Bb', 'Ca', 'Db', 'Ea', 'Fb', 'Ga', 'Hb']
Sign up to request clarification or add additional context in comments.

Comments

2

You can use zip() and cycle():

from itertools import cycle

A = ['A','B','C','D','E','F','G','H']
B = ['a', 'b']

C = [a + b for a,b in zip(A, cycle(B))]
print(C)

Output as requested.

Comments

2

More variations and benchmark:

Best 3 of 20 rounds:
 885 ns   888 ns   888 ns  [*map(concat, A, cycle(B))]
 955 ns   959 ns   961 ns  list(map(concat, A, cycle(B)))
 969 ns   976 ns   976 ns  list(map(add, A, cycle(B)))
1168 ns  1173 ns  1174 ns  list(starmap(concat, zip(A, cycle(B))))
1230 ns  1235 ns  1239 ns  list(map(''.join, zip(A, cycle(B))))
1232 ns  1236 ns  1249 ns  [a + b for a, b in zip(A, cycle(B))]
1335 ns  1344 ns  1346 ns  [f'{a}{b}' for a, b in zip(A, cycle(B))]
1359 ns  1362 ns  1366 ns  [a + B[i%2] for i, a in enumerate(A)]
1418 ns  1421 ns  1423 ns  m = len(B); [a + B[i%m] for i, a in enumerate(A)]
1492 ns  1509 ns  1510 ns  [A[i] + B[i%2] for i in range(len(A))]
1964 ns  1978 ns  1978 ns  list(map('{}{}'.format, A, cycle(B)))

With A multiplied by 1000 (i.e., 1000 times longer list):

Best 3 of 20 rounds:
 654 us   655 us   658 us  [*map(concat, A, cycle(B))]
 657 us   657 us   657 us  list(map(concat, A, cycle(B)))
 674 us   676 us   676 us  list(map(add, A, cycle(B)))
 727 us   737 us   738 us  list(starmap(concat, zip(A, cycle(B))))
 743 us   746 us   747 us  list(map(''.join, zip(A, cycle(B))))
 837 us   841 us   841 us  [a + b for a, b in zip(A, cycle(B))]
 943 us   945 us   946 us  [f'{a}{b}' for a, b in zip(A, cycle(B))]
1279 us  1288 us  1290 us  m = len(B); [a + B[i%m] for i, a in enumerate(A)]
1280 us  1280 us  1281 us  [a + B[i%2] for i, a in enumerate(A)]
1317 us  1319 us  1323 us  [A[i] + B[i%2] for i in range(len(A))]
1626 us  1633 us  1638 us  list(map('{}{}'.format, A, cycle(B)))

Code (Try it online!):

from timeit import repeat
from random import shuffle
from bisect import insort

setup = '''
from itertools import cycle, starmap
from operator import concat, add
A = ['A','B','C','D','E','F','G','H']
B = ['a','b']
'''

solutions = [
    'list(map(add, A, cycle(B)))',
    'list(map(concat, A, cycle(B)))',
    '[*map(concat, A, cycle(B))]',
    '[a + B[i%2] for i, a in enumerate(A)]',
    'm = len(B); [a + B[i%m] for i, a in enumerate(A)]',
    "list(map(''.join, zip(A, cycle(B))))",
    "list(map('{}{}'.format, A, cycle(B)))",
    '[a + b for a, b in zip(A, cycle(B))]',
    "[f'{a}{b}' for a, b in zip(A, cycle(B))]",
    'list(starmap(concat, zip(A, cycle(B))))',
    '[A[i] + B[i%2] for i in range(len(A))]',
]

exec(setup)
for sol in solutions:
    try:
        print(eval(sol) == ['Aa','Bb','Ca','Db','Ea','Fb','Ga','Hb'])
    except SyntaxError:
        pass

tss = {sol: [] for sol in solutions}
for i in range(20):
    print(f'Best 3 of {i+1} rounds:')
    shuffle(solutions)
    for sol in solutions:
        number = 10 ** 4
        t = min(repeat(sol, setup, number=number)) / number
        insort(tss[sol], t)
    for sol in sorted(tss, key=tss.get):
        print(*('%4d ns ' % (t * 1e9) for t in tss[sol][:3]), sol)
    print()

5 Comments

I've done some benchmarks and your option list(map(add, a, cycle(b))) is the fastest. It can be even faster if you replace add with concat.
@OlvinRoght Ah yes, concat. Thanks. I reran your test and now the output shows the opposite, test2(): 1.62 and test2_1(): 1.64. But I've written a more extensive test where concat indeed seems consistently a bit faster. I'll update tomorrow.
It has to be faster anyway as add will eventually end up into concat if applied to sequences, so by using concat we just removing extra layer of conditions.
@OlvinRoght Yes, that makes its "pure execution" faster, just in real benchmarks we don't get that pure execution. Added my own benchmarks with some more solutions now.
1

You can do this by using the index in the A list mapped on the number of elements in the B list with the modulo operation (%) :

A = [1,2,3,4,5,6,7,8]
B= ['a','b']
C = []

for i in range(len(A)):
    C.append(f"{A[i]}{B[i%2]}")

Comments

0

You can also try this:

A = ['A','B','C','D','E','F','G','H']
B= ['a','b']
LB = len(B)
x = 0

for i in A :
    if x>=LB :
        x=0
    print(i+B[x])
    x+=1

Comments

-1
A = [1, 2, 3, 4, 5, 6, 7, 8]
B = ["a", "b", "c", "d", "f", "g", "h", "q"]
C = []
i = 0
while i < len(A):
    for j in B:
        C.append("{}{}".format(A[i], j))   
    i = i + 1
        

print(C)

You can try this without any module.

You can specify your arrays, works anyway. I just tested like this.

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.