Skip to content

Instantly share code, notes, and snippets.

@bbookman
Created December 27, 2018 04:19
Show Gist options
  • Save bbookman/c5cf0516e9fcf0c1fdbdcff7cd43867a to your computer and use it in GitHub Desktop.
Save bbookman/c5cf0516e9fcf0c1fdbdcff7cd43867a to your computer and use it in GitHub Desktop.
Python List Comprehension: Nested
'''
Use a nested list comprehension to find all of the numbers from 1-100 that are divisible by any single digit besides 1 (2-9)
'''
#old school
no_dups = set()
for n in range(1, 100):
for x in range(2,10):
if n % x == 0:
no_dups.add(n)
print(no_dups)
print()
#nested list comprehension
result = [number for number in range(1,100) if True in [True for x in range(2,10) if number % x == 0]]
print(result)
@Folskyy
Copy link

Folskyy commented Jun 15, 2024

"""
Use a nested list comprehension to find all of the numbers from
1-1000 that are divisible by any single digit besides 1 (2-9)
"""
divisibles = [i for i in range(1, 1001) if any([1 for j in range(2,10) if not i%j])]

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