list = [1, 'one', 'first', 2, 'two', 'second', 3, 'three', 'third', 4, 'four', 'fourth', 5, 'five', 'fifth', 6, 'six', 'sixth']
is it compact way to make new list like this? (merging 3 elements, then another 3…):
['1onefirst', '2twosecond', '3threethird', '4fourfourth', '5fivefifth', '6sixsixth']
>Solution :
Using a list comprehension with conversion to string and join:
N = 3
out = [''.join(map(str, lst[i:i+N])) for i in range(0, len(lst), N)]
Output:
['1onefirst',
'2twosecond',
'3threethird',
'4fourfourth',
'5fivefifth',
'6sixsixth']