Reputation: 127
im learning list comprehension but don't know if i can add a sum two variables via list comprehension to a list. So is it possible to express below code in a list comprehension?
#Make some rolls, and store the results in a list.
#die_1 & die_2 represent D6 dices, .roll() is a .choice() generated from import random
results = []
for roll_num in range(1_000):
result = die_1.roll() + die_2.roll()
results.append(result)
Upvotes: 0
Views: 55
Reputation: 1026
The most concise way of doing this would be:
results = [ die_1.roll() + die_2.roll() for _ in range(1_000) ]
For each of the 1000 iterations, both dice are rolled and their values are summed and stored in the list.
Upvotes: 1