Skip to content

Instantly share code, notes, and snippets.

@bbookman
Created December 26, 2018 21:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bbookman/1a98da47a1a6d1ceffdd3bae7be36abe to your computer and use it in GitHub Desktop.
Save bbookman/1a98da47a1a6d1ceffdd3bae7be36abe to your computer and use it in GitHub Desktop.
Python List Comprehension: Find all of the numbers from 1-1000 that have a 3 in them
'''
Find all of the numbers from 1-1000 that have a 3 in them
'''
three = [n for n in range(0,1000) if '3' in str(n)]
print(three)
@anasauram
Copy link

anasauram commented Jan 16, 2024

These 3 options work:

x = [i for i in range(1, 1001) if str(i).find("3") != -1]
y = [i for i in range(1, 1001) if str(i).count("3") > 0]
z = [i for i in range(1, 1001) if '3' in str(i)]

print("x ->", x, len(x), "\n")
print("y ->", y, len(y), "\n")
print("z ->", z, len(z))

@SariCohen2
Copy link

You could also do it this way:
lst_include3=list(filter(lambda x: '3' in str(x), range(1,1000)))
print(lst_include3)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment