Document stub.png This article or section may not have content matching Scratch Wiki editing standards. Please improve it according to Scratch Wiki:Guidelines and Scratch Wiki:Editing Conventions. (September 2025)
Document.png Please expand this article or section. You can help by adding more information if you are an editor. More information might be found in a section of the talk page. (July 2022)

This tutorial explains how to make a two-dimensional list. A two-dimensional list is a list whose contents are also lists. Two-dimensional lists are not a feature included in Scratch, but it can be simulated with normal lists.

Method 1

Note Note: This method isn't completely a 2-Dimensional list, it is more like a list that stores lists.

This method uses data serialization to compress rows of our theoretical 2D list into strings, which are than placed in another list. For this method, you will need the lists:

(Storage::list) //This will store the strings of lists
(Compress::list)//This will help compress and decompress the lists
(Names::list)//This will store the names of the rows

You will also need the variables:

(Character
(Index
(Item
(string


To start, data serialization scripts (credit to griffpatch[1] for the code) will need to be made. We will need 2 define blocks:

define Compile (item)
...
define Decompile
...

A script to convert items into 1 string will also be nessersary

define Compile (item)
set [Index v] to (1)
repeat (length of (item))
set [Character v] to (letter (Index) of (item))
if <[|~] contains(Character)?> then
set [String v] to (join (String) [~]
end
set [String v] to (join (String) (Character)
change [Index v] by (1)
end
set [String v] to (join (String) [|]

Next, a script to decompile the string into the items is needed to be made

define Decompile
set [Item v] to ()
forever
set [Character v] to (letter (Index) of (String)
change [Index v] by (1)
if <[|] contains (Character)?> then
stop [this script v]
end
if<[~] contains (Character)?> then
set [Character v] to (letter (Index) of (String))
change [Index v] by (1)
end
set [Item v] to (join (Item)(Character))


With the data serialization scripts completed, we will now need a way to read and write the compressed rows to turn them into lists.

define Read (row)//The input 'row' is to be the name of the row
delete all of [Compress v]
set [String v] to (item (item # of (row) in [Names v]) of [Store v])
set [Index v] to (1)
Decompile::custom
repeat until <(item) = ()>
add (item) to [Compress v]
Decompile::custom

define Write (row)//The input 'row' is to be the name of the row
set [string v] to ()
repeat (length of [Compress v])
Compile (item (1) of [Compress v])::custom
delete (1) of [Compress v]
end
replace item (item # of (row) in [Names v]) of [Store v] with (String)


With the data serilisation, and read and write scripts done, we will now focus on initiating the 2D list. To start we will reset the lists:

when gf clicked
delete all of [Store v]
delete all of [Compress v]
delete all of [Names v]

Next we'll create a custom block to add, delete, insert, and replace rows in the 2D list

define Add row (name)
add (name) to [Names v]
add () to [Store v]

define Delete row (name)
delete (name) of [Names v]
delete (name) of [Store v]


define Insert row (name) at (position)
insert (name) at (position) of [Names v]
insert () at (position) of [Store v]

define Replace row (oldname) with row (name) and data (data)
replace item (item # of (oldname) in [Names v]) of [Names v] with (name)
replace item (item # of (name) in [Names v]) of [Store v] with (data)


With that done, we now create the custom blocks to edit the rows themselves.

define Add (item) to (name)
Read (name)::custom
add (item) to [Compress v]
Write (name)::custom

define Delete (item#) of (name)
Read (name)::custom
delete (position) of [Compress v]
Write (name)::custom

define Insert (item) at (position) of (name)
Read (name)::custom
insert (item) at (position) of [Compress v]
Write (name)::custom
//vvvvv more down vvvvv
define Replace item (position) of (name) with (item)
Read (name)::custom
replace item (position) of [Compress v] with (item)
Write (name)::custom


Now, all of these custom blocks mean nothing if you don't know how to use them. The following table will list the custom block, function of the block, and an effect of the block. The effects will be based on this 2D list.

2D lists original example.png



Block Role Effect
Add row [Lizard]::custom
define Add row (name)
...
This custom block adds a row to the end of the 2-Dimensional list
2D list add row custom block effect.png
Delete row [Snakes]::custom
define Delete row (name)
...
This custom block deletes a specified row
2D list delete row custom block effect.png
Insert row [Lizards] at [3]::custom
define Insert row (name) at (position)
...
This custom block inserts a row at the specified position
2D list insert row custom block effect.png
Replace row custom block for article 2D lists.png
This custom block replaces a specified row and its data
2D list replace row custom block effect.png
Add [Goose] to [Birds]::custom
define Add (item) to (name)
...
This custom block adds an item to the end of a specified row
2D list add item custom block effect.png
Delete [2] of [Cats]::custom
define Delete (position) of (name)
...
This custom block deletes an item specified by its position in a specified row
2D list delete item custom block effect.png
Insert item custom block for 2D list Article.png
This custom block inserts an item at a position in a specified row
2D list insert item custom block effect.png
Replace item custom block for 2D list article.png
This custom block replaces an item in a specified row for another item
2D list replace item custom block effect.png


Method 2

Two lists are necessary to follow the tutorial: one to store the data and another to indicate where each list is in the data.

  • (data::list)
  • (positions::list)

Entering Data

Creating a list

Before you can add data to the lists, you need to create one first by adding an item to the positions list.

add ((length of [data v]) + (1)) to [positions v]

Adding items to the last list

Note Note: This section can only add items to the last list in the list.

To add an item to the end of the last list, you can add the desired item to the end of the data list.

add [item] to [data v]

Inserting items to the last list

Note Note: This section can only insert items to the last list in the list.

To insert an item in the last list, you can add the desired item at a specific position.

insert ( ) at ((item (length of [positions v]) of [positions v]) + (position in list::custom)) of [data v]

Changing an item

You can change data by using the block in the accessing data section to find where the list item is. (x is the position of the list, and y is the position of the list inside x.)

replace item (item (((item (x::custom) of [positions v]) + (y::custom)) - (1)) of [data v]) of [data v] with ( )

Accessing Data

You can access data by checking where the first item in the list is, and then using that to find the location of the item in the specific list. (x is the position of the list, and y is the position of the list inside x.)

(item (((item (x::custom) of [positions v]) + (y::custom)) - (1)) of [data v])

Cookies help us deliver our services. By using our services, you agree to our use of cookies.