Python Forum
list comprehension - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: list comprehension (/thread-8451.html)



list comprehension - HenryJ - Feb-21-2018

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)



RE: list comprehension - buran - Feb-21-2018

yes. what have you tried - it's simple straightforward comprehension


RE: list comprehension - HenryJ - Feb-21-2018

Thanks.

I tried
results = [die_1.roll() + die_2.roll() for roll_num in range(50000)] 



RE: list comprehension - buran - Feb-21-2018

yes, that's it. Why do you think you need something else?