In Python I have a list: [11,42,122,1919, 17, 4]
and want to calculate its modulo a number (e.g. 10) to give the result as a list: [1,2,2,9,7,4]
Asked
Active
Viewed 65 times
-6
-
keep looking, and look harder :wink: – glls May 30 '16 at 09:29
-
I'm not a coder, so please just help me.. :) – J.Doe May 30 '16 at 09:30
2 Answers
1
Even though it's a homework question, you can do it as:
[a%10 for a in l]
-
not sure he knows what that means, as per his last comment "I'm not a coder, so please just help me" – glls May 30 '16 at 09:32
0
You can also do it without list comprehension, like this:
num_list = [11,42,122,1919, 17, 4]
result = []
for num in num_list:
result.append(num%10)
print(result)
# Output
[1, 2, 2, 9, 7, 4]
If you're new to python then this sort of structure with code is quite common to use.

RoadRunner
- 25,803
- 6
- 42
- 75