Trelzevir Trelzevir - 1 year ago 49
Python Question

What is the shortest way to assign different random lists to two variables?

What is the shortest way to get to two distinct lists,

, and
, which contain random integers.

I currently have:

(a,b)=([randint(0,30)for x in range(10)][randint(0,30)for x in range(10)])


a=[randint(0,30)for x in range(10)]
b=[randint(0,30)for x in range(10)]


a=b=[randint(0,30)for x in range(10)]

produces two identical lists.

Is there a shorter way to do this?

Answer Source

You can use random.sample:

from random import sample

r = range(0,30)
a, b = sample(r, 10), sample(r, 10)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download