Navigation

    CreatiCode Scratch Forum

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • CreatiCode

    Working with Lists

    2D Blocks
    1
    1
    253
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • info-creaticode
      CreatiCode last edited by admin

      Introduction

      You have learned how to make a list, edit the items in it, and read data from it. This article will cover some more advanced blocks for working with a list.

       
       

      Sorting a List

      To sort a list’s items, you can use the following block:

      w.gif

       
      Note that you can use this block for lists with text items as well. The first letter of each item will be used for comparison. For example, the word “Abby” is treated as smaller than the word “Bob”, since the letter “A” comes before “B” in the alphabet.

      When 2 items have the same first letter, then the second letter is used. For example, “Bob” is treated as smaller than “Brain”, since they both start with “B”, and “o” comes before “r” in the alphabet.

      Note that the upper case or lower case of the letters will be treated as the same.

      s1.gif

       
       

      Reshuffle a List Randomly

      You can assign a random position to each item in the list by “reshuffling” the list, similar to how we reshuffle a deck of cards.

      w.gif

       
      For another example, suppose you need to print out the name of every student in the cla*s, but you want to make it random so no one can guess who will be next. You just need to reshuffle the list of names.

      w.gif

       
       

      Reverse a List

      You can reverse a list using the “reverse” block. The first item will become the last, and the second item will become the second last, etc. If you reverse a list 2 times, you should get exactly the same list.

      s2.gif

       
       

      Join List Items into String

      You can join all items in a list together into one long string, and you can specify a separator that will be placed between these items.

      s3.gif

       
       

      Split a String into List Items

      The opposite of “joining” is splitting. You can split a long string into individual items, and you can specify which separator to use.

      s4.gif

       
       

      Append One List to Another List

      You can append a list to another list, which means adding all the items from a list to the bottom of another list. For example, when you append “numbers2” to “numbers”, the list “numbers2” won’t change, but its 3 items will be appended to the bottom of “numbers”.

      s5.gif

       
       

      Copy from One List to Another List

      You can also copy from one list to another list. The original items of the target list will be removed first, so after this operation, both lists have the same items:

      s6.gif

       
       

      Generate a Random List of Numbers

      The “pick random” block in the “Operators” category will produce a random number within a random. If you want to generate a list of random numbers, you can use the following block.

      Note that if you allow repetition, then the same number may be used more than once. If you do not allow repetition, then a number will not be used if is already in the list.

      s7.gif

       
       

      Iterate Through a List

      You can go through a list and check every item in the list.

      First, you can use the “for each item in list” block, which assigns the variable with the value of each item in the list:

       
      You can also use an index variable to visit every item in the list. The index variable will go from 1 up until it is equal to the length of the list. You can use this variable to read from the list inside this loop:

      s9.gif

      1 Reply Last reply Reply Quote 0
      • First post
        Last post