-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
  • 7,163
  • 15
  • 50
  • 93

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
  • 338,267
  • 99
  • 616
  • 750
4

Use sum() on generator:

number_of_not_204 = sum(x != 204 for x in data)
Austin
  • 25,759
  • 4
  • 25
  • 48
1

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
  • 7,677
  • 2
  • 16
  • 35