Hello all,
I’m working with a database that stores product categories and I need to create an inline keyboard for a Telegram bot based on these entries. Currently, my data is formatted in a single line with delimiters, like this:
groupA-groupB-groupC-groupD-groupE
I would like to rearrange this into a grid format, such as:
groupA groupB
groupC groupD
groupE
I am searching for a looping method or code snippet that can transform a simple list into this layout. For instance, one approach could be:
categories = ["groupA", "groupB", "groupC", "groupD", "groupE"]
layout = []
for idx in range(0, len(categories), 2):
if idx + 1 < len(categories):
layout.append([categories[idx], categories[idx+1]])
else:
layout.append([categories[idx]])
print(layout)
Any suggestions or alternative solutions to achieve this would be greatly appreciated. Thank you for your help!