Search code examples
pythonlistrandomitemskey-value-store

how can I randomize the order of a list of keys and value pairs?


I have the following list of 12 items of paired ["key", values]. For instance the first item is: ["SN00003025", 0.1]

["SN00003025", 0.1], ["SN00013002", 30000.0], ["SN00037509", 23.7],
["SN00126162", 13560.0], ["SN00155812", 7.8], ["SN00232427", 3000.0], 
["SN00316328", 12.0], ["SN00319987", 5456.0], ["SN00339436", 5600.0],
["SN00399476", 12500.0], ["SN00399477", 32.0], ["SN00399478", 1100.0]

How can I change the order of the items of this list to random?


Solution

  • You're probably looking for something like numpy.random.permutation: https://numpy.org/doc/stable/reference/random/generated/numpy.random.permutation.html

    import numpy as np
    lst = # ... Your list
    permuted = np.random.permutation(lst)