I am running my code through an example set: [9,20,2,3] I have been using the modulo operator (%) and my code is:
if index < len(the_list):
for m in the_list:
print(m)
if m % 3 == 0:
a = True
else:
a = False
return a
else:
return False
Args:
the_list (list): The input list
index (int): The index
Returns:
_type_: True: if the value is divisible by 3,
False: if the value is not divisible by 3,
False: if the index is invalid
One additional condition I must include is if the index position is larger than the length of the list to return False, but I believe I already have that properly integrated.
CodePudding user response:
You can just use a list comprehension.
[(num%3 == 0) for num in myList]
CodePudding user response:
here is a function that could help you :
def divisible_by_three(numbers):
for number in numbers:
if number % 3 == 0:
return True
else:
return False
CodePudding user response:
This should work, just replace myList with your actual list
myList = [9,20,2,3]
finalList = []
for x in myList:
if x % 3 == 0:
finalList.append(True)
else:
finalList.append(False)
print(finalList)