ChardScript List
call myList = ["apple", "banana", "cherry"]
List
Lists are used to store multiple items in a single variable.
Lists are created using square brackets:
Example
Create a List:
call thisList = ["apple", "banana", "cherry"]
echo(thisList)
List Items
List items can add new item values, extend list to list and allow duplicate values.
List items are indexed, the first item has index [0]
, the second item has index [1]
etc.
Append
To appends an element to the end of the list, use append()
function:
Example
call theList = ["Hello World!"]
append(theList, "Hi World!")
echo(theList)
# Output: Hello World!, Hi World!
Extend
To add the specified list elements (or any iterable) to the end of the current list, use extend()
function:
Example
call list1 = ["apple", "banana"]
call list2 = ["cherry", "mango"]
extend(list2, list1)
echo(list2)
# Output: cherry, mango, apple, banana
Allow Duplicates
Since lists are indexed, lists can have items with the same value:
Example
call thislist = ["apple", "banana", "apple", "banana"]
echo(thislist)
List Length
To determine how many items a list has, use the length()
function:
Example
call thislist = ["apple", "banana", "cherry"]
echo(length(thislist))
List Items - Data Types
List items can be of any data type:
Example
String, int and boolean data types:
call list1 = ["apple", "banana", "cherry"]
call list2 = [1, 5, 7, 9, 3]
call list3 = [true, false, false]
A list can contain different data types:
Example
A list with strings, integers and boolean values:
call list1 = ["abc", 34, true, 40, "male"]
Last updated