You can make your code a bit nicer by iterating explicitly through groups of four instead of element-by-element. There are a couple of ways of doing this. The easiest would just be to increment the index by four:
card_sequence = ...
for index in range(0, len(cardSequence), 4):
four_card = card_sequence[index:index + 4]
# process four_card
This has the advantage of not calling the processing code on an empty four_card
when index == 0
, as your code does. Also, I would recommend sticking to one naming convention (underscores vs CamelCase). I picked the former since that is the generally recommended Python convention.
You can do a slightly more complicated version using the grouper
recipe provided by the itertools
documentation. The advantage of this approach is that it will probably scale better:
def grouper(iterable, n, fillvalue=None):
"Collect data into fixed-length chunks or blocks"
# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return zip_longest(*args, fillvalue=fillvalue)
card_sequence = ...
for four_card in grouper(card_sequence, 4):
four_card = list(four_card)
# Process four_card
Keep in mind that this implementation of grouper
is going to fill extra elements in the last group with None
if your input sequence does not have a multiple of four elements. It also assumes that you consume all the elements of the group in each iteration (which you do in this case). For alternative implementations, see this wonderful answer.