Python Forum
random.shuffle(list) produces empty list - why?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
random.shuffle(list) produces empty list - why?
#1
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':
Reply
#2
random.shuffle changes the list in place instead of returning a shuffled list.
Reply
#3
(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
Reply
#4
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]
Reply
#5
OK! That makes sense! Thank you very much!

- Henrik
Reply
#6
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?
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#7
(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)
Recommended Tutorials:
Reply
#8
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Code with empty list not executing adeana 9 3,735 Dec-11-2023, 08:27 AM
Last Post: buran
  Sample random, unique string pairs from a list without repetitions walterwhite 1 457 Nov-19-2023, 10:07 PM
Last Post: deanhystad
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,172 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,540 May-01-2023, 09:06 PM
Last Post: deanhystad
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,211 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  List all possibilities of a nested-list by flattened lists sparkt 1 919 Feb-23-2023, 02:21 PM
Last Post: sparkt
  List of random numbers astral_travel 17 2,713 Dec-02-2022, 10:37 PM
Last Post: deanhystad
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 1,838 Oct-26-2022, 04:03 PM
Last Post: deanhystad
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,519 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
Question Keyword to build list from list of objects? pfdjhfuys 3 1,568 Aug-06-2022, 11:39 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020