0
grid = [['.', '.', '.', '.', '.', '.'],
        ['.', 'O', 'O', '.', '.', '.']]

I would like to ask if how can I count the values inside the list. In this case, the value would be 6 not 2. Sorry I am not that familiar with the terms. Thanks!

Lyle
  • 3
  • 1
  • 5

2 Answers2

1

If you want the length of each nested list you can use a list comprehension

[len(sub_list) for sub_list in grid]

[6, 6]

If you want the unique values of the lengths, you can use a set-comprehension

{len(sub_list) for sub_list in grid}

{6}

Maarten Fabré
  • 6,938
  • 1
  • 17
  • 36
0

this is a list of lists. It would actually look like this, just adding a bracket to the end:

grid = [
['.', '.', '.', '.', '.', '.'],
['.', 'O', 'O', '.', '.', '.']
]

This other question has the answer.

  • Yes. It is list of list. I wasn't able to add 1 bracket at the last. My bad. Thank you. – Lyle Jun 22 '17 at 20:53