How to shuffle a sublist?

Greeting to all, I am trying to make a random sublist, where I have a complex list that has more than one sub-list inside, each sublist has different range of numbers and I want to shuffle them by using List.Shuffle, and each time I try to get the same shuffle, not different for each sub-list.

Capture

You can see sublist 0, 1 and 2 they are all have the same shuffled values.even when I change to L3, L2 and L1 inside the Shuffle node.

Many thanks in advance

Check this thread out: https://github.com/DynamoDS/Dynamo/issues/7801
I believe its an issue with Dynamo.

1 Like

perhaps Math.RandomList and RemapRange as a work-around?

1 Like

I do thank you dear, I tried it and it worked perfectly Many thanks dear again

I tried Math.RandomList as it always gives empty list, I will try the RemapRange, thanks in advance

Math.RemapRange will change one range into another without shuffle nor randomize,
Thanks JacobSmall, you always help me when I post I do appreciate your effort, you are the best, I tried the solution given by Jackchaskell, via python and it works, even thu I know nothing in Python, hoping to find a direct Dynamo solution,
Cheers mate

1 Like

Was thinking that if you called a random list you’d be able to shuffle using a remap and sort index by value, but it winds up that the same list called N times will return the same random too… :frowning:

Glad we have a working python solution.

1 Like

Hi Jackchaskell,
I used the python script and it worked for 2 days, I tested on 2 laptops!! now one laptop seems to give me error and not allowing me to use the python script while the other still working smoothly!! any idea please?
Capture of error in python

Hello @firas007noori,

You could try the following Python code and see if it suits your purpose :slight_smile:

# Load the sys module
import sys
# Append the Iron Python standard library
sys.path.append(r'C:\Program Files (x86)\IronPython 2.7\Lib')
# Import the 'random' module that contains the 'shuffle' function
import random

# Allow data to enter the node and assign it to the 'data' variable
data = IN[0]

# Check if the data coming in to the Python node isn't a list. If it's not, make it one
if not isinstance(data, list):
    data = [IN[0]]

# The 'shuffle' function works on place in a list, so we don't need to reassign it. Rather we simply call it on our existing list.
random.shuffle(data)

# Then we assing the input list, now 'shuffled' to our OUT port
OUT = data

It should shuffle your list data - and then reshuffle every time your data changes into the Python node.

2 Likes

I will try it dear Solamour, thanks

1 Like

I still got the list without any shuffle, I had no clue whats going on!! hahaha, sorry to disturb you


I need to randomize the sublist!! and you can see the one I got from you is giving me the same, thanks in advance

This is due to the way Python works - you need to specify how deep a list is (i.e it’s Rank) manually - or through some very fancy methods that I didn’t make in this set :slight_smile:

However, you can leverage the awesome framework we have in Custom Nodes to get around this and properly manage your data.

In the example below you see three groups:

  1. The blue group as my previous example. Works perfectly fine for a single list!
  2. The pink group - same code as before. Will shuffle the entire list order, but noto the contents at the deepest level! Useful in some situations - but not this one :slight_smile:
  3. The cyan group which contains the same code as before, but simply wrapped into a Custom Node.

To do this, take a copy of the Python Node, select it and navigate to: Edit -> Create Custom Node, or use the shortcut of Ctrl + D.

This will pop up the Custom Node Properties window. It contains three fields to fill in with data:

  1. Your node name (This will appear on the node itself)
  2. Your nodes description (This will appear if you hover on the node itself)
  3. Your Add-ons Category (This will be where it appears in the Library and each period will create a new sub-menu item. In our case you would look for: Helpers -> List -> Find ‘myShuffle’)

When you type this data in, due to how the syntax of the Python Node works you will get an error message - as below. You can safely ignore this as we will fix the problem in a second! :slight_smile: It also makes your custom node exist (As seen in the image below also) that is currently in it’s yellow warning state.

This will take you into a new mode, the Custom Node Editor window, which is yellow in the background. It will also have auto-generated two new nodes, an Input node and an output node; these correlate to the input and output ports generated on your new node and are also the current source of our warning state! Click OK on this warning and then double-click on your new custom node if it doesn’t automatically open.

You’ll now see your new Custom Node Editor mode in a new tab - as shown below:

You’ll have your input port in a red error state and your output port in a good state - both with default names taken from the ports of the Python node.

To finish creating the Custom Node and fixing the error we proceed as such:

  1. Rename the input port from: IN[0] to list : var[]..[]
  2. Rename the output port from: OUT to shuffledList (Albeit this is discretionary!)

This will change the error state into a good state for our input port by stipulating it’s new port name of list and it’s ability to parse data of any arbitrary length, stipulated by the DesignScript syntax of : var[]..[] where the var means any data type can come through (i.e strings, numbers, elements etc.) and the rank of those lists can be any type (When you get deeper into DesignScript you can find the other forcing functions of single list, double list etc.).

Then click on File --> Save As and save your new Custom Node (Note that it saves with a file extension of .dyf). This will save into your AppData/Roaming/Dynamo/Dynamo Core/2.X/definitions folder - and this is where your custom nodes live :slight_smile:

After you have saved, you can click X on your Custom Node tab and go back to your Dynamo Graph.

The final step to do is enable List@Level on the Custom Node, which is done by clicking on the list input port chevron and selecting Use Levels. It will default to Level2 which is what we want in this case - and voila! You now have your sublists being randomly shuffled.

You’ll also be able to find this node (Note: On your machine only unless you share this file with your colleagues) in your library too!

4 Likes

I will run throu that tomorrow, I can not thank you enough, you are great mate, i do appreciate your effort, Can you advise me to a good reference or book or even a YouTube Channel to learn Python for Dynamo!!?? I would really be thankful, I have my YouTube Channel and teach most of what I learned for free for others, I am also an instructor teaching CGI at Uni, Sorry I talked to much, happy to know such a kind person,
Cheers

2 Likes

You are most welcome :smiley: There are various learning resources out there for Python, specifically for Dynamo as it’s a little different (The way it talks to Dynamo and the use of IronPython - a flavour of Python that can talk to C# environments).

Check out the following:

  1. The Python section in the Dynamo Primer: https://primer.dynamobim.org/10_Custom-Nodes/10-4_Python.html
  2. @oliver.green’s new Python primer (Coummunity driven): https://dynamopythonprimer.gitbook.io/dynamo-python-primer/
  3. My AU London course: https://github.com/Amoursol/dynamoPython/blob/master/images/DivingDeeper_ABeginnersLookAtPythonInDynamo_AU_London2018.pdf
  4. Just have a search on here! Lots of awesome forum posts on the topic :slight_smile:
5 Likes