How to find how many number contain a certain say 7) integer in a list from 1 to 9999

Viewed 63
nums = [i for i in range(1,10000)]

b=[x for x in nums if '7' in x ]

print (b)

TypeError: argument of type 'int' is not iterable

3 Answers

Create a Boolean generator for the given condition, and call sum on it, you will get the number of integers that evaluates to be True for the given condition.

>>>sum('7' in str(i) for i in range(1,10000))
3439
nums = [i for i in range(1,10000)]

b=[x for x in nums if '7' in str(x) ]

print (len(b))
print(b)
nums = [i for i in range(1,100)]

b=[x for x in str(nums) if '7' in x]

print(f'There are {len(b)} numbers that contain the number 7.')
Related