Ring/Lessons/Lists

Lists
In this chapter we are going to learn how to deal with lists.

Create Lists
We can create new lists by defining the list items inside square bracts.

Example:

Also we can create new lists using the : operator

Example:

Also we can create lists using the list function

Syntax:

Example

.. note:: the list index start from 1

Add Items
To add new items to the list, we can use the Add function.

Syntax:

Example:

Also we can do that using the + operator.

Syntax:

Example:

Get List Size
We can get the list size using the len function

Syntax:

Example:

Delete Item From List
To delete an item from the list, we can use the del function

Syntax:

Example:

Get List Item
To get an item from the list, we uses the next syntax

Example:

Set List Item
To set the value of an item inside the list, we can use the next syntax

Example:

Search
To find an item inside the list we can use the find function

Syntax:

Example:

Example:

Also we can use the binarysearch function to search in sorted list.

Syntax:

Example:

Output:

Sort
We can sort the list using the sort function.

Syntax:

Example:

We can sort list of strings

Example:

We can sort a list based on a specific column.

Example:

Output:

Reverse
We can reverse a list using the reverse function.

Syntax:

Example:

Insert Items
To insert an item in the list we can use the insert function.

Syntax:

The inserted item will be after the index

Example:

Nested Lists
The list may contain other lists

Example:

Copy Lists
We can copy lists (including nested lists) using the Assignment operator.

Example:

First-class lists
Lists are `first-class citizens `_ where we can store lists in variables, pass lists to functions, and return lists from functions.

Example:

Using Lists during definition
We can use the list items while we are defining the list for the first time.

Example:

Passing Lists to Functions
Lists are passed to functions by reference, This means that the called function will work on the same list and can modify it.

Example:

Access List Items by String Index
Instead of using numbers to determine the item index when we get item value or set item value, We can access items using string index if the item is a list contains two items and the first item is a string.

Example:

This type of lists can be defined in a better syntax using the : and = operators.

Example:

.. tip:: using : before identifier (one word) means literal

.. note:: using = inside list definition create a list of two items where the first item is the left side and the second item is the right side.

We can add new items to the list using the string index

Example:

Passing Parameters Using List
This type of lists is very good for passing parameters to functions Where the order of parameters will not be important (we can change the order).

Also some parameters maybe optional.

Example: