-2

How do you get the number of items that are NOT 204 in this list?

data = [204, 204, 204, 500, 204, 204, 500, 500, 204, 404]

number_of_not_204 = any(x != 204 for x in data)

# returns True
print number_of_not_204

# looking to get the number 4 (500, 500, 500, and 404 are not 204)
Jonathan Kittell
  • 6,475
  • 13
  • 48
  • 87

3 Answers3

6

You are describing the basic usage of built-in function sum:

>>> data = [204, 204, 204, 500, 204, 204, 500, 500, 204, 404] 
>>> sum(1 for n in data if n != 204) 
4
wim
  • 302,178
  • 90
  • 548
  • 690
3

Use sum() on generator:

number_of_not_204 = sum(x != 204 for x in data)
Austin
  • 25,142
  • 4
  • 21
  • 46
0

You can use len() on new list without 204:

data = [204, 204, 204, 500, 204, 204, 500, 500, 204, 404]
x = len([i for i in data if i != 204])
Olvin Roght
  • 6,675
  • 2
  • 14
  • 32