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
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()
list(map(add, a, cycle(b))) is the fastest. It can be even faster if you replace add with concat.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.add will eventually end up into concat if applied to sequences, so by using concat we just removing extra layer of conditions.
1asupposed to be? Do you mean'1a'string?