7

I have sequence of played cards in a list. There were 4 players, so each four elements in the list represent a single trick. I have to process 4 cards together to find trick winner. I need to split the list four elements at a time. I am currently doing the following:

cardSequnce = [
    'D7', 'D8', 'DT', 'DA',
    'H2', 'H7', 'HK', 'H5',
    'H3', 'HT', 'HA', 'HQ',
    'H8', 'D2', 'H4', 'HJ',
    'D6', 'D3'
]
four_card = []
for index, card in enumerate(cardSequnce):
    if(index % 4 == 0):
        # process four_card
        four_card = []
    four_card.append(card)

I think that I can do better than this using power of python. Can I access 4 cards together in a loop? Can any one help me to make my code more Pythonic?

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
avni hirpara
  • 384
  • 2
  • 3
  • 10
  • Try the `grouper` recipe from [`itertools`](https://docs.python.org/3/library/itertools.html#itertools-recipes). – Mad Physicist Jul 14 '17 at 14:43
  • 1
    Possible duplicate of [What is the most "pythonic" way to iterate over a list in chunks?](https://stackoverflow.com/questions/434287/what-is-the-most-pythonic-way-to-iterate-over-a-list-in-chunks) – Mad Physicist Jul 14 '17 at 14:58

3 Answers3

13

If you want to create lists of length of 4 from a list, this would do the trick:

[cardSequence[i:i + 4] for i in range(0, len(cardSequence), 4)]
Hannu
  • 11,685
  • 4
  • 35
  • 51
2
offset = 4
index = 0
while index < len(YourList):
       result = YourList[index:index+offset]
       index += offset
pooya
  • 155
  • 6
  • This is definitely much less Pythonic than using `range` in a `for` loop, but probably still better than OP's original solution. – Mad Physicist Jul 14 '17 at 14:52
2

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.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264