1
OriginalName = ["a", "b", "c"]

ShuffleList = random.shuffle(OriginalName)
print ShuffleList

When I run the above, it returns as 'None'. I am trying to create a new list once it has been shuffled.

I want to create another list that has been created randomly and may resemble ["c", "a", "b"]

ettanany
  • 19,038
  • 9
  • 47
  • 63
  • 1
    Thre's a similar question here: http://stackoverflow.com/questions/976882/shuffling-a-list-of-objects-in-python `random.shuffle` does this in place so returns None and changes the original data - either copy it or use ` random.sample(OriginalName, len(OriginalName))` instead – doctorlove Dec 15 '16 at 16:02

3 Answers3

9

random.shuffle is re-organizing the original list - you are only working with one list.

Try something like

OriginalName = ["a", "b", "c"]
ShuffleList = list(OriginalName)  # making a new list copy

random.shuffle(ShuffleList)

Now you have two lists, the original and shuffled.

Side note: make sure to use snake case (original_name instead of OriginalName). It's Python convention.

Martin Konecny
  • 57,827
  • 19
  • 139
  • 159
  • I want another list, that has been created randomly, and will resemble something like `["c", "a", "b"]`. Is this possible? –  Dec 15 '16 at 15:58
  • Yes, `ShuffeList` is a copy of your original list, which is then shuffled in the answer. – Martin Konecny Dec 15 '16 at 16:18
4

Create the new list first, then shuffle that.

shuffled = OriginalName[:]
random.shuffle(shuffled)
print shuffled
Holloway
  • 6,412
  • 1
  • 26
  • 33
1

I would use random.sample for this

import random
l = list(range(5))
print(random.sample(l, len(l)))

prints something like

[4, 0, 2, 1, 3]
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96