# Function to shuffle a list completely

**URL:** <https://discourse.psychopy.org/t/function-to-shuffle-a-list-completely/23255>\
**Category:** Online experiments\
**Created:** [May 28, 2021, 3:56pm UTC](https://discourse.psychopy.org/t/function-to-shuffle-a-list-completely/23255 "2021-05-28T15:56:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mengsili](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@mengsili](https://discourse.psychopy.org/u/mengsili)\
**Post date:** [May 28, 2021, 3:56pm UTC](https://discourse.psychopy.org/t/function-to-shuffle-a-list-completely/23255/1 "2021-05-28T15:56:06Z")

</div>

I need a function to shuffle a list e.g. a = [1, 2, 3, 4] so that the order becomes completely different (the number at each position is different from the original one). I find the following code (with some modifications), but the zip() function cannot be translated to JS automatically.

```auto
import random

def shuffle_list(some_list):
    randomized_list = some_list[:]
    while True:
        random.shuffle(randomized_list)
        for a, b in zip(some_list, randomized_list):
            if a == b:
                break
        else:
            return randomized_list

```

I tried to modify it as the follow but it can only partly shuffle the order:

```auto
def myFun_shuffle_sequence_order(study_order_list, sl):
    test_order_list = study_order_list[:]
    while True:
        shuffle(test_order_list)
        for i in range(sl):
            for j in range(sl):
                if i == j and test_order_list[i] == study_order_list[j]:
                    break
        else:
            return test_order_list

shuffle_sequence_order = myFun_shuffle_sequence_order

```

I’d appreciate if there is any suggestion on how to accomplish this both offline and online! Thanks as always.

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [May 29, 2021, 9:02am UTC](https://discourse.psychopy.org/t/function-to-shuffle-a-list-completely/23255/2 "2021-05-29T09:02:48Z")

</div>

Are you avoiding shuffle(myList) because some elements may end up in the unchanged?

---

<div class="post-metadata">

**Author:** ![mengsili](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@mengsili](https://discourse.psychopy.org/u/mengsili)\
**Post date:** [May 29, 2021, 1:41pm UTC](https://discourse.psychopy.org/t/function-to-shuffle-a-list-completely/23255/3 "2021-05-29T13:41:45Z")

</div>

Yes, I want it to be completely shuffled so that the number at each position is different from the old one.

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [May 29, 2021, 3:56pm UTC](https://discourse.psychopy.org/t/function-to-shuffle-a-list-completely/23255/4 "2021-05-29T15:56:11Z")

</div>

> [@mengsili](#):
>
> ```auto
> for i in range(sl):
> for j in range(sl):
> if i == j and test_order_list[i] == study_order_list[j]:
> break
> 
> ```

Shouldn’t this be

```auto
for i in range(sl):
          if test_order_list[i] == study_order_list[i]:
              break

```

---

<div class="post-metadata">

**Author:** ![mengsili](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@mengsili](https://discourse.psychopy.org/u/mengsili)\
**Post date:** [May 29, 2021, 6:02pm UTC](https://discourse.psychopy.org/t/function-to-shuffle-a-list-completely/23255/5 "2021-05-29T18:02:35Z")

</div>

Oh yes! Seems it’s working. Sorry I overthought. Thanks!

---

<div class="post-metadata">

**Author:** ![mengsili](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@mengsili](https://discourse.psychopy.org/u/mengsili)\
**Post date:** [June 4, 2021, 8:24am UTC](https://discourse.psychopy.org/t/function-to-shuffle-a-list-completely/23255/6 "2021-06-04T08:24:25Z")

</div>

> [@mengsili](#):
>
> ```auto
> def myFun_shuffle_sequence_order(study_order_list, sl):
> test_order_list = study_order_list[:]
> while True:
> shuffle(test_order_list)
> for i in range(sl):
> for j in range(sl):
> if i == j and test_order_list[i] == study_order_list[j]:
> break
> else:
> return test_order_list
> 
> shuffle_sequence_order = myFun_shuffle_sequence_order
> 
> ```

Hi Wake, sorry to bother you again-do you have any idea how to translate this code to JS? With the automatic translation the online exp got stuck with no error. I tried to modify it but cannot make it run. Or is there an easier way to accomplish my goal? Thanks!

This is my Python code, offline, it suffles a list completely with no problem.

```auto
def myFun_shuffle_sequence_order(study_order_list, sl):
    test_order_list = study_order_list[:]
    while True:
        shuffle(test_order_list)
        for i in range(sl):
            if test_order_list[i] == study_order_list[i]:
                break
        else:
            return test_order_list

shuffle_sequence_order = myFun_shuffle_sequence_order

```

The automatic translated JS code (I search online JS does not have the for/else loop as in Python, that’s might be the problem):

```auto
function myFun_shuffle_sequence_order(study_order_list, sl) {
    var test_order_list;
    test_order_list = study_order_list.slice(0);
    while (true) {
        shuffle(test_order_list);
        for (var i, _pj_c = 0, _pj_a = range(sl), _pj_b = _pj_a.length; (_pj_c < _pj_b); _pj_c += 1) {
            i = _pj_a[_pj_c];
            if ((test_order_list[i] === study_order_list[i])) {
                break;
            }
        }
    }
}
shuffle_sequence_order = myFun_shuffle_sequence_order;

```
