# How to count number of 0 in a list of lists?

## Question:

I want calculate the number of time a 0 appears in a "list of lists" web scraped, it’s like my `list` of lists is not a `list` because when I print, it’s done that :

``````









``````

if it’s really is a list of list it’s should tell me this :

``````[,,,,,,,,,]
``````

So, `list` or not `list` I cant count the number of time the 0 is here, how can I do for count the 0 ?

In this exemple, there is two 0, so the result is = 2

There is my code, maybe you can tell me how I can, I tried count(0) and other method, but it’s dont work

``````from bs4 import BeautifulSoup
import requests
from csv import writer

url= "https://blablablabla.htm"
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')

lists = soup.find_all('tr', class_="mat1")

with open('file.csv', 'w', encoding='utf8', newline='') as f:
thewriter = writer(f)

for list in lists:

but = list.find('td', class_="tj1").text.replace('xa0', '').replace(" ", "1")

info = but[3:]

infos = len(info)
infoss = int(infos)
print(infoss)

``````

print(infoss) is :

``````3
1
3
3
1
1
0
0
1
1
``````

and i need a code which tell me the number of 0 in this list, so here it’s 2

You are already iterating through the list of lists and printing out the values within it.

Can you think of a way to check the value instead of printing it and keep track of the number of times the value is equal to 0?

EDIT: Are you saying that this code doesn’t work?

``````import requests
from csv import writer

url= "https://blablablabla.htm"
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')

lists = soup.find_all('tr', class_="mat1")

with open('file.csv', 'w', encoding='utf8', newline='') as f:
thewriter = writer(f)

coun = 0
for list in lists:

but = list.find('td', class_="tj1").text.replace('xa0', '').replace(" ", "1")

info = but[3:]

infos = len(info)
infoss = int(infos)
print(infoss)

if infoss == 0 :
coun = coun + 1

print(coun)
``````

i tried this :

``````from bs4 import BeautifulSoup
import requests
from csv import writer

url= "https://blablablabla.htm"
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')

lists = soup.find_all('tr', class_="mat1")

with open('file.csv', 'w', encoding='utf8', newline='') as f:
thewriter = writer(f)

for list in lists:

but = list.find('td', class_="tj1").text.replace('xa0', '').replace(" ", "1")

info = but[3:]

infos = len(info)
infoss = int(infos)
print(infoss)

coun = 0
if infoss < 1 :
coun = coun + 1

print(coun)

``````

the result is this :

``````0
0
0
0
0
0
1
1
0
0
``````

yes it’s tell me 1 and 1, but i dont want that form of answer, i just want it’s tell me result = 2, without all the rest

i tried to just have the number of item with a len, but it’s seems to me, it’s a bunch of list, but they are not in big list as they should to be. I cant track nothing, they seems not linked betwwen them in a big list.

I tried a dico method, and doesnt work too

You could try creating new list to store value of `infoss` then count element `infoss` that has 0.

``````lst_infoss = []
for list in lists:

but = list.find('td', class_="tj1").text.replace('xa0', '').replace(" ", "1")

info = but[3:]

infos = len(info)
infoss = int(infos)
print(infoss)
# append infoss to new list (lst_infoss)
lst_infoss.append(infoss)

print(lst_infoss)
zero_infoss = lst_infoss.count(0)
print(zero_infoss)
``````

You were close to your goal and based on question title and your second example you could also use a `list comprehension` iterating your `list` and check the `len()` of result:

``````ls = [,,,,,,,,,]
len([l for l in ls if l == 0])
#output -> 2
``````

Note: Avoid using `python` reserved terms (`keywords`), this could have unwanted effects on the results of your code.

Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.