Trelzevir Trelzevir - 27 days ago 6
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,

a
, and
b
, 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)])


or

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


Using

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


produces two identical lists.

Is there a shorter way to do this?

Answer

You can use random.sample:

from random import sample

r = range(0,30)
a, b = sample(r, 10), sample(r, 10)