Skip to main content

List and Map

ZWL support various operations on list and map types. Following is an overview of some of the operations. See API Reference for all the available functions and their detail.

Creating and manipulating a List#

# Create list using list literalfruits = ['apple', 'banana', 'orange']
# Initialize a variable to an empty List and add itemsfruits = []addTo(fruits, 'mango')addTo(fruits, 'plum')addTo(fruits, 'grape')print(fruits) # prints [mango, plum, grape]
# Set item at indexfruits = ['apple', 'banana', 'orange']setAt(fruits, 1, 'peach')print(fruits[1]) # prints, peach
# Remove an itemfruits = ['apple', 'banana', 'orange']removeFrom(fruits, 'apple')print(fruits) # prints [banana, orange]
# Remove item at indexfruits = ['apple', 'banana', 'orange']removeAt(fruits, 1)print(fruits) # prints [apple, orange]
# Clear listfruits = ['apple', 'banana', 'orange']removeAll(fruits)print(fruits) # prints []

Creating and manipulating a Map#

# Create map using map literalproduct = {  name: 'iPhone',  model: '8',  price: 600}
# Initialize a variable to an empty map and add key-value pairsproduct = {}putIn(product, 'availability', 30)putIn(product, 'readyToShip', true)putIn(product, 'shipInternationally', false)print(product) # prints {availability: 30, readyToShip: true, shipInternationally: false}
# Set value of a keyproduct = {  name: 'iPhone',  model: '8',  price: 600}putIn(product, 'name', 'mac')putIn(product, 'model', 'pro')putIn(product, 'price', 1200)print(product) # prints {name: mac, model: pro, price: 1200}
# Remove by keyproduct = {  name: 'iPhone',  model: '8',  price: 600}removeFrom(product, 'model')print(product) # prints {name: iPhone, price: 600}
# Clear mapproduct = {name: 'nest'}removeAll(product)print(product) # prints {}

Get size of List and Map#

fruits = ['apple', 'banana', 'orange']print(length(fruits)) # prints 3
product = {  name: 'iPhone',  model: '8'}print(length(product)) # prints 2