I have a simple problem and I couldn't solve it. I have a list;
[9, 0, 3, 0, 0, 0, 2, 0, 6]
If an element in this list is in digits, I want to add 1 to counter variable.
digits = [1,2,3,4,5,6,7,8,9]
lst = [9, 0, 3, 0, 0, 0, 2, 0, 6]
Right now I'm doing it with
digits = [1,2,3,4,5,6,7,8,9]
lst = [9, 0, 3, 0, 0, 0, 2, 0, 6]
counter = 0
for x in lst:
if x in digits:
counter += 1
I want to write that for loop as one-liner. I tried
t = counter += 1 for x in lst if x in digits
But not worked as expected. I just stuck, how can I do this?
[x for x in lst if x in digits]and add 1 to counter variable? I triedcounter+=1 for x....as I wrote in my question but didn't work.