List Chunks Sample
COMMENT
Sample on how to break down a large list into smaller chunks
- @supermamon
for: https://www.reddit.com/r/shortcuts/comments/gd42iy/iterating_over_a_large_list_is_extremely_slow/
COMMENT
create a random list
MATHS
Random number between
10
and
120

Random Number
SCRIPTING
Repeat
Random Number

Repeat Results
TEXT
Item
Repeat Index

Text
VARIABLES
Add
Text
to
list
SCRIPTING
End Repeat

Repeat Results
COMMENT
make smaller chunks
COMMENT
set the maximum number of items to process in a batch
NUMBER
25

Number
VARIABLES
Set variable
threshold
to
Number
COMMENT
calculate the number of batches needed to process the whole list
SCRIPTING
Count
Items
in
list

Count
VARIABLES
Set variable
list_size
to
ItemCount
MATHS
list_size
…
Number
Options Under Construction

Calculation Result
MATHS
list_size
-
remainder
Options Under Construction

Calculation Result
MATHS
Calculation Result
÷
threshold
Options Under Construction

Calculation Result
MATHS
Calculation Result
+
1
Options Under Construction

Calculation Result
VARIABLES
Set variable
iterations
to
Calculation Result
COMMENT
start processing
NUMBER
1

Number
VARIABLES
Set variable
start
to
Number
SCRIPTING
Repeat
iterations

Repeat Results
VARIABLES
Set variable
batch_number
to
Repeat Index
COMMENT
find out which section of the list to process. this will result into the `start` and `end` variables
MATHS
start
+
threshold
Options Under Construction

Calculation Result
MATHS
Calculation Result
-
1
Options Under Construction

Calculation Result
VARIABLES
Set variable
end
to
Calculation Result
SCRIPTING
If
end
is greater than
list_size
VARIABLES
Set variable
end
to
list_size
SCRIPTING
End If

If Result
COMMENT
get the portion of the list and process them

Item from List
SCRIPTING
Repeat with each item in
Item from List

Repeat Results
VARIABLES
Set variable
current_item
to
Repeat Item 2
COMMENT
Do what you need to do with the current_item here

Text
VARIABLES
Add
Text
to
results
SCRIPTING
End Repeat

Repeat Results
COMMENT
move on to the next batch by adjusting the start index to the next number
MATHS
end
+
1
Options Under Construction

Calculation Result
VARIABLES
Set variable
start
to
Calculation Result
SCRIPTING
End Repeat

Repeat Results
TEXT
Combine
results
with
New Lines

Combined Text

Text
Showcuts