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?
#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


Messages In This Thread
RE: random.shuffle(list) produces empty list - why? - by Yoriz - Oct-16-2016, 05:46 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Entry field random pull from list, each return own line Bear1981 6 796 Feb-25-2025, 06:09 AM
Last Post: Pedroski55
  what to return for an empty list Skaperen 2 1,195 May-24-2024, 05:17 PM
Last Post: Skaperen
  Strange behavior list of list mmhmjanssen 3 1,617 May-09-2024, 11:32 AM
Last Post: mmhmjanssen
  Code with empty list not executing adeana 9 5,785 Dec-11-2023, 08:27 AM
Last Post: buran
  Sample random, unique string pairs from a list without repetitions walterwhite 1 1,892 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 2,608 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 3,351 May-01-2023, 09:06 PM
Last Post: deanhystad
  find random numbers that are = to the first 2 number of a list. Frankduc 23 7,137 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  List all possibilities of a nested-list by flattened lists sparkt 1 1,780 Feb-23-2023, 02:21 PM
Last Post: sparkt
  List of random numbers astral_travel 17 5,711 Dec-02-2022, 10:37 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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