Fran Ahedo Guerrero
Fran Ahedo Guerrero

Reputation: 67

How do I generate a random list WITH REPETITION in Python?

I want to generate a random list with repetition and I do not know how it is done, because it always gives me error, I write this:

  1. x=rm.sample(range(1001), 2000)
  2. print(x)

I want a list of 2000 numbers between 1 and 100

Help me pls.

Upvotes: 2

Views: 7528

Answers (1)

Brandon Deo
Brandon Deo

Reputation: 4305

You can just use the random library and a list comprehension.

import random
[random.randint(1,100) for x in range(2000)]

If you'd like to print that list, you can assign it a value and print it as you've done in your question.

import random
x = [random.randint(1,100) for x in range(2000)]
print x

EDIT: Based on OP's comment below: If you'd like to remove all values under 50, you can do it while you're generating the initial list and not generate them at all.

import random
[random.randint(50,100) for x in range(2000)]

Upvotes: 7

Related Questions