Python Forum

Full Version: list comprehension
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

Is it possible to convert the below for loop to a list comprehension and get "results"?

for roll_num in range(50000):
    result = die_1.roll() + die_2.roll()
    results.append(result)
yes. what have you tried - it's simple straightforward comprehension
Thanks.

I tried
results = [die_1.roll() + die_2.roll() for roll_num in range(50000)] 
yes, that's it. Why do you think you need something else?