<jupyter_start><jupyter_text>![Callysto.ca Banner](https://github.com/callysto/curriculum-notebooks/blob/master/callysto-notebook-banner-top.jpg?raw=true)  Turtles: Lists and DictionariesThis notebook will use Turtles to explore some ways to structure data in Python. ListsA [Python list](https://docs.python.org/3/tutorial/datastructures.html) contains values in a specific order. In the previous notebook we used a list of colors, `colors = ['purple', 'blue', 'red']` and referenced individual colors from that list, for example `colors[0]` was purple.Run the following code cell to have a turtle draw a 50 pixel line for each color in the list.<jupyter_code># declare a list of colors
colors = ['purple', 'blue', 'red']

from mobilechelonian import Turtle
t = Turtle()
t.speed(10)

# run this loop for each value in the list of colors
for color in colors:
    t.pencolor(color) # set the pen color
    t.forward(50) # draw a line<jupyter_output><empty_output><jupyter_text>Each of those colors was a string, but a list can also contain other data types such as integers (whole numbers).In the next code cell, add some integers to the `turtle_moves` list with commas between them (notice that we don't use `''` around integers).Then run the cell to see what happens.<jupyter_code># declare a list of integers
turtle_moves = [30, 20, 50, 40, 100]

t = Turtle()
t.speed(10)

# for each integer in the list, move forward and turn right
for distance in turtle_moves:
    t.forward(distance)
    t.right(30)<jupyter_output><empty_output><jupyter_text>Instead of using "`for` item `in` list" to loop through a list we can also use index number (starting from zero). One advantage of this is that we can interate through multiple lists in the same loop.In the code cell below you will use `i` as the index, and loop through `range(3)` meaning `i=0`, `i=1`, `i=2` (up to but not including 3).Paste each the following list index calls between the appropriate brackets in the loop, e.g. `t.pencolor(colors[i])`.`angles[i]``colors[i]``distances[i]`<jupyter_code># declare the three lists, each with three values
colors = ['purple', 'blue', 'red']
distances = [60, 80, 100]
angles = [90, 144, 127]

t = Turtle()
t.speed(10)

for i in range(3):
    t.pencolor()  # paste  colors[i]  between the brackets
    t.forward()  # paste  distances[i]  between the brackets
    t.right()  # paste  angles[i]  between the brackets<jupyter_output><empty_output><jupyter_text>Use the next cell to have a turtle draw a [parallelogram](https://en.wikipedia.org/wiki/Parallelogram) with the angles `120` and `60`, and four sides with a distance of `50` pixels each.<jupyter_code># declare lists for the four angles and four lengths in a parallelogram
# the angles should be 120, 60, 120, 60 and distances should be 50, 50, 50, 50
angles = []
distances = []

# create a variable that is the number of values in the angles list
number_of_angles = len(angles)

t = Turtle()
t.speed(10)

# do the loop once for each value in the number of angles
for i in range(number_of_angles):
    t.right(angles[i])
    t.forward(distances[i])<jupyter_output><empty_output><jupyter_text>We can even create a list of lists and then reference parts of it. In this example the `parallelogram` list contains a list of angles and a list of distances. Try to write a comment before each line in the following code to describe what it does.<jupyter_code># 
parallelogram = [[120, 60, 120, 60], [80, 50, 80, 50]]
# 
angles = parallelogram[0]
# 
distances = parallelogram[1]
# 
number_of_angles = len(angles)
# 
t = Turtle()
# 
t.speed(10)
# 
for i in range(number_of_angles):
    # 
    t.right(angles[i])
    # 
    t.forward(distances[i])<jupyter_output><empty_output><jupyter_text>Here is another way of using nested lists, each list with the `parallelogram` list contains an angle and a distance.To access values in nested lists, the syntax is `list[which_list][which_item]`.So if `letters = [['a', 'b'], ['c','d']]` then `letters[1][0]` is 'c'.In our example, the angles are `parallelogram[i][0]` and the distances are `parallelogram[i][1]`. Add those in the brackets for `t.right()` and `t.forward()` then run the cell.<jupyter_code># declare a nested list for drawing a parallelogram
parallelogram = [[120,80], [60,50], [120,80], [60,50]]

t = Turtle()
t.speed(10)

# loop through the list once for each list in the parallelogram list
for i in range(len(parallelogram)):
    t.right()  # paste  parallelogram[i][0]  between the brackets
    t.forward()  # paste  parallelogram[i][1]  between the brackets<jupyter_output><empty_output><jupyter_text>DictionariesIn a paper dictionary, each word has its own definition (or multiple definitions). When we want to know the definition of a word, we find that word in the dictionary.In a similar way, Python [dictionaries](https://docs.python.org/3/tutorial/datastructures.htmldictionaries) identify values by a word (which is called the [key](https://wiki.python.org/moin/DictionaryKeys)). We use curly brackets rather than square brackets to create a dictionary.Add a line of code to the next cell that declares a dictionary called "move_one" and has the keys "right" and "forward". For example:`move_one = {'right':120, 'forward':80}`<jupyter_code># paste in the line that declares a  move_one  dictionary


t = Turtle()
t.speed(10)

# make the moves from the dictionary values
t.right(move_one['right'])
t.forward(move_one['forward'])<jupyter_output><empty_output><jupyter_text>Here is another way of reading from a dictionary, using `if` statements. We are also introducing the key `color`.<jupyter_code># declare a dictionary with the keys color, right, and forward
move_one = {'color':'red', 'right':120, 'forward':80}

t = Turtle()
t.speed(10)

# loop through the items (keys and values) in the dictionary
for key, value in move_one.items():
    if key == 'color':
        t.pencolor(value)
    if key == 'right':
        t.right(value)
    if key == 'forward':
        t.forward(value)<jupyter_output><empty_output><jupyter_text>To be able to use that code for dictionaries whenever we want, let's define it as a function.<jupyter_code># define the function
def turtle_move(move_dictionary):
    # loop through the items in the dictionary
    for key, value in move_dictionary.items():
        if key == 'color':
            t.pencolor(value)
        if key == 'right':
            t.right(value)
        if key == 'left':
            t.left(value)
        if key == 'forward':
            t.forward(value)
        if key == 'backward':
            t.backward(value)
print('Function defined, now you can call it with  turtle_move(move_dictionary)')<jupyter_output><empty_output><jupyter_text>Now use that function by adding the line`turtle_move(move_dictionary)`at the end of the next cell.<jupyter_code>t = Turtle()
t.speed(10)

# declare a dictionary
move_dictionary = {'color':'red', 'right':120, 'forward':80}

# paste the line  turtle_move(move_dictionary)  below to call the function<jupyter_output><empty_output><jupyter_text>Next let's create a list of dictionaries. Notice that it can extent to multiple lines if we have a new line after a bracket or comma. The list still starts and ends with a square bracket.Notice that each dictionary doesn't need to contain all of the keys, and any unknown keys are ignored by our function.<jupyter_code># declare the list of dictionaries
list_of_moves = [
    {'color':'red', 'right':120, 'forward':80},
    {'color':'green', 'right':60, 'forward':50},
    {'color':'blue', 'right':120, 'forward':80},
    {'color':'yellow', 'right':60, 'forward':50},
    {'color':'black'},
    {'distance':100, 'thing':404}
    ]

t = Turtle()
t.speed(10)

# loop through the list of dictionaries
for move in list_of_moves:
    turtle_move(move)<jupyter_output><empty_output><jupyter_text>ChallengeUsing what you now know about lists and dictionaries, and using the `turtle_move(move_dictionary)` function, make a list of moves to draw a star.As a hint, in each move your turtle should go forward a certain amount (e.g. `80`) then either turn right `120` or left `48`.The first two moves in the list are done for you.<jupyter_code>list_of_moves = [{'color':'yellow', 'forward':80, 'right':120},
                 {'forward':80, 'left':48},
                 
                ]

t = Turtle()
t.speed(10)

for move in list_of_moves:
    turtle_move(move)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11232486345286155, 'fraction_numerical': 0.02220375207789124, 'mean_word_length': 4.4944553163731245, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '24946', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 2866, 'n_tokens_neox': 2621, 'n_words': 997, 'path': 'TMDictionaryTurtles/turtles-and-dictionaries-student.ipynb', 'repo_name': 'callysto/sask-ambassadors', 'size': '8422'}