Creation, Traversal, Insertion and Removal Lists: Creation, Traversal, Insertion and Removal
What is a list? Very similar to a string, it is a sequence of values. These values are called elements or items. The list ‘remembers’ the order, index and name of each element. Sometimes referred to as an array
students = [“Dion”, “Meghan”, “Joseph”, “Denise” ] To create a list Let’s begin by naming the list, just like we would a variable. We then ‘list’ all the elements, using brackets, commas and quotation marks. Single or double quotations marks work ( ‘ ‘ or “ “) students = [“Dion”, “Meghan”, “Joseph”, “Denise” ] As the students are entered in the list, they are indexed 0th, 1th, 2th, et cetera. (we don’t use ‘1st’ because 0 is the ‘first’ position. 1 is 2nd pos.) Lists, just like variables in Python must be lowercase and not contain any ‘keywords’ such as “print”, “and”, “input”, etc.
What works for Strings works for lists Each element / item in a list is also indexed, just like in a string. We can find the length, index of a character, and a specific character or word in a list. We can concatenate and repeat just like we can with a string. Concatenate means to bring together (without adding). For example: If I were to concatenate the numbers 11 and 42, I would get the return 1142, not 53 like we would if we added.
Concatenate (con*cat*en*nate) Adding without arithmetic 4 + 5 = 9 We can add using integers But can we add words together? “Yellow” + “Blue” = “Green”? No. “Yellow” + ”Blue” = “YellowBlue” This is concatenation.
How to concatenate: We use the + using strings to combine them. This is not addition. “4”+”3” would not return 7. This would return 43. We can use this to combine lists
String functions work for lists too… Strings functions: len(str) Returns the length in characters, of a string str[ : ] ‘slices’ or returns a piece of the string from [___ to ____] str.find( ) Finds a character / word within a string
Also… We can concatenate using the + sign “string 1” + “string 2” We can repeat a string or a list using the * symbol “string 1” * 3 will result in: string1 string1 string1
To add to the list Let’s say we want to add a few more students to our current list: students = [“Dion”, “Meghan”, “Joseph”, “Denise” ] # now we will append or add elements to this list. Typing: students.append(“Chris”) students.append(“Janet”) students.append(“James”)
To add to the list Will give us our complete list: students = [“Dion”, “Meghan”, “Joseph”, “Denise”, “Chris”, “Janet”, “James” ] Notice that the last elements (students) were added to the end of the list.
Remove an item We can use the del command to remove an item from a list Let’s look at our student list: students = [“Dion”, “Meghan”, “Joseph”, “Denise” ] If we wanted to remove Joseph from the list, we would type: del students[2] Meaning delete from students list, the item at index 2 remember that Python begins counting at index 0.
Now, Let’s manipulate the list 1) Find the length (in elements) of the list. 2) Find the index position of a specific student – “Janet” 3) Traverse the list so that each item in the list is printed one line at a time. * This may be tricky to do. How could we use a for loop to accomplish this? Talk out how you would do this first.