In some cases we need to store multiple values within a single variable.
For example:
A grocery list
- item_1 = "Eggs"
- item_2 = "Apples"
- item_3 = "Tomatoes"
- item_4 = "Bread"
Instead of storing each item in the above list in a new variable we can make use of a List.
A List allows us to store multiple values together in a sequence. Lists are defined using the square brackets []
. The data values within these brackets are separated using a comma ,
.
As you can see from this example, grocery_list
is a variable containing a list of four String values each representing an item in the grocery list.