Search code examples
pythonlistpython-2.7unordered

Unordered list generation in python


I'd like to create a million unordered list, xrange(1, 1000000) for eg gives me an ordered list. I think I need something like xrange but generates it in an unordered fashion. I think I can generate a list manually by looping around random.randint and some manual checks to guarantee uniqueness of number in list but I reckon it would be time consuming. Any ideas?


Solution

  • import random
    L = range(1, 1000000)
    random.shuffle(L)  # shuffles in-place
    

    on Python3, you will need to use

    L = list(range(1, 1000000))
    random.shuffle(L)  # shuffles in-place