I need to be able to count how many of the string \"O\" is in my list
top_board = [
[None, None, None, None, None, None, None, None, None],
[None, None,
Try this:
sum(x.count("O") for x in top_board)
if [j for i in top_board for j in i].count('O'):
print "O is present in the list"
cnt = sum([lst.count('O') for lst in top_board])
# then do something depending on cnt
def count_O(l):
count = 0
for sublist in l:
count += sublist.count("O")
return count
if count_O(top_board) == 0:
#do something
Update
sum([sum([1 for x in y if x == "O"]) for y in top_board])
(hadn't notice the nesting...)
Since you asked for a function:
def count_O (top_board):
if True in ["O" in e for e in top_board]:
print "O found"