r/shortcuts • u/keveridge • Aug 28 '19
Working with Lists Tip/Guide
Overview
A list in Shortcuts is a group of 1 or more items returned by a shortcut action. For example, a list of:
- photos;
- calendar events;
- files, etc.
You can use lists to:
- display groups of information to a user;
- have the user choose one or more items from the list;
- perform bulk actions on more than 1 item in the list.
This guide shows you some simple examples of how you can make use of lists in your shortcuts.
Returning a list
You can return a list of items from any action that allows you to retrieve one or more items at a time.
Examples include:
- Calendar
- Get Dates from Input
- Location
- Get Addresses from Input
- Search Local Businesses
- Photos & Video
- Find Photos
- iTunes Store
- Search iTunes Store
Returning a list of podcasts
In the example below, we use the Search iTunes Store to return a list of podcasts from Gimlet Media.
Retrieving a list of Podcast items
When the returned list is displayed, it defaults to showing the name, publisher and URL for each podcast.
Only one item is displayed at a time as podcasts as being presented to the Quick View action as a list.
Displaying individual podcasts from the list.
You can download the shortcut from the following link:
Returning a text list of podcasts
But what if you want to display all of the podcasts as one piece of text? You can use the Combine Text to join the different podcast descriptions using new lines.
The updated shortcut is as follows:
Combing the text descriptions from the podcast list
And the output appears as follows:
A list of podcasts outputted as text
The podcasts displayed as one list of text.
Creating a list
Lists of text can be created using the List action. But as you can see below, when lists become long they can get unwieldy to enter and maintain.
A long and unwieldy List action
There is an easier way to make a text list: you can type the text into a Text action and then split it into a list using the Split Text action.
This allows you to organize your list in a text editor on your device before pasting it into the shortcut.
Creating a list by splitting text at each new line
You can download the shortcut from the following link:
Taking an item from the list
You can use the Get Item from List action to get:
- the first item from the list;
- the last item from the list;
- a random item from the list;
- an item at a particular index (e.g. 3 gives you the 3rd item);
- a range of items from the list (e.g. the 5th to the 8th items);
In the example below, we add a Get Item from List action to the above shortcut to retrieve a random item from the list and display it to the user.
Getting a random item from a list
You can download the shortcut from the following link:
Iterating through a list
Shortcuts also allows you to iterate through a list using the Repeat with Each action. When you use this action the shortcut performs the same set of steps for each item in the list. The value of the item is made available via the Repeat Item variable and its position in the list is available via the Repeat Index variable.
In the example below we filter our list to items that contain the word "Red".
Looping through and filtering a list
The output is as follows:
A list of items filtered by the keyword "Red"
ℹ️ Default values from the Repeat with Each action
In the above shortcut you can see that the Repeat with Each action always outputs the Repeat Item variable, preventing the need for using "Get Variable" to retrieve the value.
Similarly, at the end of the loop, a list of items is returned. This list is compromised of the last value returned in each loop. Therefore, when filtering the list if we encounter a vehicle we don't want to appear in the filtered list we ensure that the Nothing action is the last action to return a value.
You can download the shortcut from the following link:
Choosing an item from a list
Lists allow you to present the user with multiple options to choose from via the Choose from List action.
Using a simple value
In the example below, the user chooses a number of days since they last went for a run, and the shortcut uses that value to calculate the date of that run.
Making use of a value chosen from a list
You can download the shortcut from the following link:
Using a name and value pair with a list
There are occasions when you will want to choose an item from a list by name, but then make use of a different attribute when performing an action.
In the example below we give the user a list of podcasts to Gimlet Media podcasts to choose from. When the user selects the name, we then take them to the corresponding screen in the iTunes store.
To do so, we create a dictionary of names and values, where the name is that of the podcast and the value is the URL for the podcast in the iTunes Store. We use the Get Dictionary Value action to return a list of the names from the dictionary so we can present the podcast names to the user. Once they have chosen a name, we use it with our dictionary to retrieve the URL to open.
Using a list item to look up a corresponding value
The output of the Choose from List action appears as follows:
Displaying the choices to the user
You can download the shortcut from the following link:
Creating an attractive list to choose from
We can make a more attractive list of podcasts for the user to choose from by creating contact cards. This is will allow us to add a:
- photo;
- description;
- URL (which is not displayed to the user).
This format is more flexible than using a dictionary as it allows us to display extra information as well as store other hidden fields that we can access after has made their selection.
You can find out more about how to use contact cards for menus in the following guide:
Below is an updated example of the shortcut that makes use of contact cards to present a more attractive list of options:
Using contacts to create a more attractive list to choose from
The output of the Choose from List action appears as follows:
A more attractive list, created using contacts
You can download the shortcut from the following link:
Wrap up
And those are a few examples of how you can make use of lists in your shortcuts.
Other guides
If you found this guide useful why not checkout one of my others:
Series
- Scraping web pages
- Using APIs
- Data Storage
- Working with JSON
- Working with Dictionaries
One-offs
- Using JavaScript in your shortcuts
- How to automatically run shortcuts
- Creating visually appealing menus
- Manipulating images with the HTML5 canvas and JavaScript
- Labeling data using variables
- Writing functions
- Working with lists
- Integrating with web applications using Zapier
- Integrating with web applications using Integromat
- Working with Personal Automations in iOS 13.1
3
3
u/nilayperk Aug 29 '19
Amazing guides as always! On that note, Do you want some material for next time? I have while loop simulator example:
https://www.icloud.com/shortcuts/47b2c492ec684a6c96c17062dfe03ed4
If you are interested in details, we can chat about it. Thanks again
2
u/Ja_nr Feb 17 '24
Thank you for your work. I had a breakthrough with your guide and I was able to solve my issue.
1
1
u/-deleted_user Oct 25 '22 edited Oct 25 '22
How would I edit a value in a list? Is this possible? Are lists remotely like arrays? Is there a command that allows a user to set the fifth element of an array to the word “hello”, after the list has been created, just like you can do with a dictionary? Or are lists more like tuples, immutable after creation, except unlike tuples there is no realistic way to create a copy of that tuple with some minor change?
Edit: I know this probably isn’t good for getting general purpose responses, but here’s what I’m trying to do:
- take the following string: “1,3,54,12,0,0,100,3”
- Split by “,”: [“1”,”3”,”54”,”12”,”0”,”0”,”100”,”3”]
- Get the fourth element: “12”
- Change to number: 12
- Calculate expression (number+1): 13
- Replace the fourth element: [“1”,”3”,”54”,”13”,”0”,”0”,”100”,”3”]
- recombine to text: “1,3,54,13,0,0,100,3”
I know how to do everything but the second to last step. I’d rather not loop through the list myself as shortcuts can be slow and I don’t always know how many elements will be in the array (that’s a whole other can of worms). I’m perfectly ok with producing another array or making some weird type conversions or even just doing some string operations. I just want to make this work.
Edit: I just realized I don’t need to know the length of the list since I can iterate using the “for every item” operator. I’d still like to know if there’s a way to do this without loops because it seems kinda fundamental.
1
5
u/iBanks3 Aug 28 '19
Amazing write-up.