2

this is my code :

import random

a = [12,2,3,4,5,33,14,124,55,233,565]
b=[]
for i in a:
    b.append(random.choice(a))

print a,b

but i think maybe has a method like sort named randomList

has this method in python .

thanks

4 Answers 4

5
import random

a = [12,2,3,4,5,33,14,124,55,233,565]

b = a[:]
random.shuffle(b)

# b: [55, 12, 33, 5, 565, 3, 233, 2, 124, 4, 14]

This will not modify a.

To modify a inplace, just do random.shuffle(a).

Sign up to request clarification or add additional context in comments.

Comments

3

I think you are looking for random.shuffle.

Comments

2

you could use random.shuffle

random.shuffle(a)

would give a random order of a.

Comments

1
>>> random.sample(a, len(a))

[14, 124, 565, 233, 55, 12, 5, 33, 4, 3, 2]

this has several advantages over random.shuffle:

All elements of a are part of the returned list. See more here.

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.