0

i want to create a list of random integers

I want the value of num to be a random integer The following line of code gives me a syntax error invalid syntax

i just want to know that is there any way to do this using list comprehension

numbers = [num for num in range(1,6) num = random.randint(1,10)]

2 Answers 2

2

Looking at your requirements, you aren't required to assign num. To generate a list of random numbers simply do:

numbers = [random.randint(1,10) for _ in range(1,6)]

You can alternatively do:

numbers = random.sample(range(1,11),k=5) # k is the repeating count
Sign up to request clarification or add additional context in comments.

2 Comments

Surely k=5 would be closer to OP's requirement
@Cobra true, didn't give that a look.
1

You don't need the num variable, just use the random.randint() call.

numbers = [random.randint(1,10) for _ in range(1,6)]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.