Here is an example of how to build a list.
mylist = [] mylist.append(1) mylist.append(2) mylist.append(3) print(mylist[0]) # prints 1 print(mylist[1]) # prints 2 print(mylist[2]) # prints 3 print("\n" + str(type(mylist)) + "\n") print(mylist)
Just press Run!
I sneakly snuck str(type(mylist)) into this example - as an example to cast one type of object to a string to get it to print cleanly.


Accessing an index which does not exist generates an exception (an error). Remember: Lists start indexing at 0.
mylist = [] mylist.append(1) mylist.append(2) mylist.append(3) print(mylist[10]) # prints ??? - nothing but an error. #changing 10 to 0,1,2 fixes this.
Just press Run!



Exercise:

In this exercise, you will need to add numbers and strings to the correct lists using the "append" list method.
numbers = [] strings = [] names = ["John", "Eric", "Jessica"] # write your code here second_name = None # this code should write out the filled arrays and the second name in the names list (Eric). print(numbers) print(strings) print("The second name on the names list is %s" % second_name) numbers = [] strings = [] names = ["John", "Eric", "Jessica"] # write your code here numbers.append(1) numbers.append(2) numbers.append(3) strings.append("hello") strings.append("world") second_name = names[1] # this code should write out the filled arrays and the second name in the names list (Eric). print(numbers) print(strings) print("The second name on the names list is %s" % second_name) test_output_contains("[1,2,3]", no_output_msg= "Make sure that you have added the 3 values to the `numbers` list.") test_output_contains("['hello', 'world']", no_output_msg= "Make sure that you have added the `strings`.") test_output_contains("The second name on the names list is Eric", no_output_msg= "Did you fill in the variable `second_name` with the second name in the names list?") success_msg("Great Job!")
Look at the top examples to access and add to lists!