...

/

Working with Lists

Working with Lists

Let’s get introduced to some of the properties and methods of a list.

As discussed, a List is a type of object that has particular properties and particular methods that it can perform. Let’s look at some of them below.

Indexing

Lists use zero-based indexing. This means that the first element of a list is located at the 0th index.

Since each element has its own position, a list can contain duplicates of a single element because each duplicate is still unique in its position. For instance, we can have a list with five identical elements as shown below.

Accessing an element

To access an element at a particular index we can use square brackets ([]).

The general syntax is as follows:

Press + to interact
listName[index]

Let’s look at an example below.

Press + to interact
main() {
var listOfVegetables = ['potato', 'carrot', 'cucumber'];
print(listOfVegetables[1]);
}

In the code snippet above, we are accessing the second element of the list listOfVegetables, which is the element at the index 1.

Finding the length of a list

The length of a list is simply the number of elements in that list. To find the length of a list, ...

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy