0

My question is I have a list from 0 - 17 and I want to create a 7 value array in random, in which each value in the array is not duplicate in python?

1
  • 1
    Does the array contain duplicates? Commented Oct 12, 2015 at 23:41

2 Answers 2

3

You want random.sample

In [22]: L
Out[22]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]

In [23]: random.sample(L, 7)
Out[23]: [6, 7, 2, 16, 10, 0, 3]
Sign up to request clarification or add additional context in comments.

Comments

2

Try this:

import random
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
random.shuffle(lst)
lst[:7]

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.