13,420
edits
Changes
→PART 3 - Iterating Over Lists
:This last section demonstrates an extremely useful for lists: the ability to quickly '''loop through every value in the list'''. '''For loops''' have a set number of times they loop. The '''for''' loop (a "determinant" loop) will execute all indented code for each item (element) in the list. Using loops with list allow for efficient processing of stored data.
::Let's take a moment to understand how the '''for''' loop works. The for loop will store data within a temporary variable (in the demonstration below, the variable called: '''item''') and run code that are indented below the for loop. This operation will repeat until ALL contained items within the list have been used.<br><br>
:::*The list '''my_list''' should have the values: '''1, 2, 3, 4, 5'''
:::*The script should have a function called '''add_item_to_list(my_list)'''<dd><dl>The above This function takes a single argument which is a list name itself. It will then look at the value of the last existing item in the list, it will then append a new value that is one unit bigger (i.e. '''+1''' and modifying that same list without returning any value).</dl></dd>
:::*The script should have a function called '''remove_items_from_list(my_list, items_to_remove)'''<dd><dl>This function takes two arguments: a list, and a list of numbers to remove from the list. This function will then check if those items exist within that list, and if they exist, then they will be removed. This function will modify the list without returning any value.</dl></dd>