16. Accessing the list

In the previous lesson we created a list with 5 items. Now we will learn some basic commands that will allow us to manage the list and access its items. Let’s begin with verifying that our 'listfive' exists by printing it. If the list doesn’t exist, please repeat the previous lesson to create it.
Print the list 'listfive'
Now we have a list with 5 items. When we created the list we told the logo turtle, please put the string “first in the first cell, the string “second in the second cell ... , and the string “fifth in the fifth cell. The command to access the list items is: item NUMBER_OF_CELL LIST_NAME. For example, to print the second item in our list we will write: print item 2 :listfive.
Print the third item of 'listfive'
We have learned before the random command. random 5 will output a number between 0-4. Lets say we want to print a number between 1-5 , how can we do it? We should use the sum command along with random.
Print a random number between 1-5 using sum and random command
We can use the random command to print a random element from the list. Remember that the index of the first list item is 1 and not 0.
Print a random element from list five
We already know how to print a random item from a list, but there is also a command that can make our life easier. We can use the command pick LIST_NAME in order to pick a random item from a list.
Print a random item from the 'listfive' using pick try to do it few times to see you are getting different results
The command butfirst will output all the list items except for the first item. We will use the command shortcut bf.
Print all the listfive items except for the first item using butfirst
The command butlast will output all the list items except for the last item. We will use the command shortcut bl.
Print all the listfive items except for the last item using butlast
TurtleAcademy learn programming for freeYour browser is not supporting canvasWe recomand you to use Chrome or Firefox browsers