We want to make a block that swaps two items in a list. The block takes two numbers, representing positions of items in the list, and the list itself as its inputs; it swaps the positions of those items in the list. Make this block in the List category; it's a command block. Set the input types of itemX and itemY to number, and the type of data to list.

'swap items' block

When you click Apply, you should be able to see the block with two round number input spots and one list input spot.

'swap items' block with list input

(By the way, we called the list input data instead of list because people have commented that it's too easy to confuse a list variable block with the block to create an empty list: orange list variablered list constructor .)

Finish entering the complete script:

complete swap items script

Here is an example of how the block will be used: If we run the block with the arguments

Swap items

where game board is the list

Before

then the list should become

After

The replace item blocks in this script are an example of a new technique, list mutation--changing items in an existing list, instead of creating a new list the way map and keep do. As in this example, mutation can be more efficient than recopying the entire list except for the two changed items. On the other hand, mutation introduces the possibility of new kinds of bugs that can't happen with functional programming, the technique we've been using with lists until now. If two parts of a large program are using the same list for different purposes, and one part mutates the list, the other part will get confused. That's why we showed you functional programming first, and why functional programming should be your first instinct.

We have used mutation before with non-list data; for example, the for block changes the value of its variable i each time through the loop. Mutation isn't so dangerous in that context, because that variable exists only inside the for loop, there's no way another part of the program can be relying on an old value. Mutating global variables does have the same risk, though, which is why we've been saying that script variables are a better choice when possible. But a game board is an example of a value that's unavoidably both global and mutable. We'll go into this further in the next lesson.