Python Forum

Full Version: random.shuffle(list) produces empty list - why?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi

In the sample program I have attached below I first use
SelectedTargets = random.sample(TargetPool, NoOfTargetsToSelect)
to draw out some of the items from a list. Then I want to shuffle that list with
Sessions2 = random.shuffle(SelectedTargets)
. But that gives me an empty list containing only "None". Why?

I can add that I use Python 3.5 (32-bit) through PyCharm Professional 2016.2.3 on Windows 10 64-bit.

I look forward to hear from you!

Best regards,
Henrik R.

Here is the whole program:
import random

TargetPoolFile = open('TestPool.txt', 'r')
TargetPool = TargetPoolFile.readlines()
NoOfTargetsInPool = len(TargetPool)
print("No. of targets in Pool:", NoOfTargetsInPool)
NoOfTargetsToSelect = 5
print("No. of targets to select:", NoOfTargetsToSelect)

SelectedTargets = random.sample(TargetPool, NoOfTargetsToSelect)
print(SelectedTargets)
# Until here: It works
# According to https://docs.python.org/3.5/library/random.html:
# random.shuffle(list) should rearrange the order of the items on
# the list, but instead it gives an empty list - containing "None":
Sessions2 = random.shuffle(SelectedTargets)
print(Sessions2)
Here is the contents of the input-file 'TestPool.txt':
random.shuffle changes the list in place instead of returning a shuffled list.
(Oct-16-2016, 05:32 PM)Yoriz Wrote: [ -> ]random.shuffle changes the list in place instead of returning a shuffled list.

Thank you for your reply!
English is not my mother tongue, so what do you mean by "changes the list in place"?

Henrik
SelectedTargets is shuffled. The function does not return anything it alters the passed in object.

Example
import random
selected_targets = [1, 2, 3, 4, 5]
random.shuffle(selected_targets)
print(selected_targets)
Output:
[1, 2, 5, 4, 3]
If you want to keep selected_targets as it is and get a new shuffled list, you can use sample with the full list size.
target_pool = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
no_of_targets_to_select = 5
selected_targets = random.sample(target_pool, no_of_targets_to_select)
print(selected_targets)
sessions2 = random.sample(selected_targets, len(selected_targets))
print(sessions2)
Output:
[4, 7, 6, 5, 9] [9, 4, 6, 7, 5]
OK! That makes sense! Thank you very much!

- Henrik
Quote:I can add that I use Python 3.5 (32-bit) through PyCharm Professional 2016.2.3 on Windows 10 64-bit. 

May I ask why you are using 32 bit Python on a 64 bit operating system?
(Oct-16-2016, 06:32 PM)sparkz_alot Wrote: [ -> ]May I ask why you are using 32 bit Python on a 64 bit operating system?
I do this all the time because some 3rd party libs have trouble with 64 bit libraries. (ei. python + pygame + py2exe often have problems with 64 bit when working together, and ends up being easier to just use 32 bit regardless of what the OS is)
(Oct-16-2016, 06:32 PM)sparkz_alot Wrote: [ -> ]
Quote:I can add that I use Python 3.5 (32-bit) through PyCharm Professional 2016.2.3 on Windows 10 64-bit. 

May I ask why you are using 32 bit Python on a 64 bit operating system?

When I followed the instructions on how/where to download Python on python.org I was led to that version.
I did notice it was 'only' 32-bit, but didn't want to spend more time on why.