This course is currently a work in progress. Please join the waitlist to receive regular updates.
Adding values to a list
How to add values to a list?
The values can be added to a list using the add()
method. Following is an example of adding values to a list of strings.
Loading...
In the above example:
- The keyword
List
is used to declare a list which can hold string values. - The variable name
names
can be used to operate (add/remove..etc) on the list. - The
names
list contain three values(Adam, Akash, Chen).
How to access an element from a list?
The values in a list can be accessed using their index
. The first element is at index 0, the second element is at index 1, and so on. The last element's index is equal to the list's size minus 1.
From the above names
list:
- The
names[0]
will return Adam. - The
names[1]
will return Akash. - The
names[2]
will return Chen.
Try It Yourself
Loading...